Re: [Boston.pm] version of perl that can use scalar filehandles

2006-05-23 Thread Jerrad Pierce
Note that 5.6 says *auto-vivification*, you could actually use scalar file
handles prior to this but you had to use Symbol::gensym (which is the proper
way to do the work-around given).

perl -MModule::CoreList -e 'print Module::CoreList->first_release('Symbol');'

prints 5.002
-- 
H4sICNoBwDoAA3NpZwA9jbsNwDAIRHumuC4NklvXTOD0KSJEnwU8fHz4Q8M9i3sGzkS7BBrm
OkCTwsycb4S3DloZuMIYeXpLFqw5LaMhXC2ymhreVXNWMw9YGuAYdfmAbwomoPSyFJuFn2x8
Opr8bBBidcc=
--
MOTD on Pungenday, the 70th of Discord, in the YOLD 3172:
1 1 2 3
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] version of perl that can use scalar filehandles

2006-05-23 Thread Ben Tilly
On 5/23/06, Uri Guttman <[EMAIL PROTECTED]> wrote:
> > "BT" == Ben Tilly <[EMAIL PROTECTED]> writes:
>
>   BT> If you didn't remember the do local trick, you could always use Symbol
>   BT> and then call gensym to get your typeglob.
>
> i have used Symbol::gensym for years and it is fine for this. it comes
> with perl5 from way back before 5.6 (not sure how old it is). in fact i
> still use it in some modules as i want them to be backward compatible
> with older perls.
>
>   BT> As for passing old-style filehandles, both of the following syntaxes
>   BT> are likely to work:
>
>   BT>   call_function(*FILEHANDLE);
>   BT>   call_function(\*FILEHANDLE);
>
> i prefer the ref version but inside the called sub it won't make a
> difference and the code will mostly be the same.
>
> but there is one difference which is whether you can do OO calls on the
> handle. you may need to load IO::Handle (or one of its many subclasses)
> to get that support.

That isn't a difference, at least not with current versions of Perl.
You can do OO calls on the handle after using either syntax to pass it
in.  Which methods are available depends on what modules have been
loaded.

  perl -le 'sub foo {$fh = shift; $fh->print("hello")} foo(*STDOUT)'
  perl -MFileHandle -le 'sub foo {$fh = shift; $fh->print("hello")}
foo(*STDOUT)'

I don't know whether that flexibility goes back to Perl 5.005 though.

Cheers,
Ben
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] version of perl that can use scalar filehandles

2006-05-23 Thread Uri Guttman
> "BT" == Ben Tilly <[EMAIL PROTECTED]> writes:

  BT> If you didn't remember the do local trick, you could always use Symbol
  BT> and then call gensym to get your typeglob.

i have used Symbol::gensym for years and it is fine for this. it comes
with perl5 from way back before 5.6 (not sure how old it is). in fact i
still use it in some modules as i want them to be backward compatible
with older perls.

  BT> As for passing old-style filehandles, both of the following syntaxes
  BT> are likely to work:

  BT>   call_function(*FILEHANDLE);
  BT>   call_function(\*FILEHANDLE);

i prefer the ref version but inside the called sub it won't make a
difference and the code will mostly be the same.

but there is one difference which is whether you can do OO calls on the
handle. you may need to load IO::Handle (or one of its many subclasses)
to get that support.

uri

-- 
Uri Guttman  --  [EMAIL PROTECTED]   http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs    http://jobs.perl.org
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] version of perl that can use scalar filehandles

2006-05-23 Thread Ben Tilly
Do not weep.

What changed in 5.6 was that it started autovivifying them.  Just make
the following conversion:

  open(my $fh, $file) ...

  my $fh = do {local *FH};
  open($fh, $file) ...

and your problem is fixed.

Cheers,
Ben

On 5/23/06, Greg London <[EMAIL PROTECTED]> wrote:
> 5.6?
>
> (weeps)
>
> well, that'll never happen.
>
> I'll have to recode with *GLOBS.
>
> (weeps some more)
>
> Thanks for all the replies.
>
> Greg
>
> 
>
> From: Ricker, William [mailto:[EMAIL PROTECTED]
> Sent: Tue 5/23/2006 4:23 PM
> To: Greg London
> Cc: boston-pm@mail.pm.org
> Subject: RE: [Boston.pm] version of perl that can use scalar filehandles
>
>
>
> > more importantly, what is the syntax for passing a filehandle
> > into a routine if it is FILEHANDLE instead of $FILEHANDLE?
>
>open(FILEHANDLE, ">>$filename" ) or die "trying $!";
>
>
> > open(my $fh, "filename");
>
> Autovivification of unitialized scalar filehandles was added in 5.6.0
>http://search.cpan.org/~nwclark/perl-5.8.8/pod/perl56delta.pod
>
> 
> File and directory handles can be autovivified
>
> Similar to how constructs such as $x->[0] autovivify a reference, handle
> constructors (open(), opendir(), pipe(), socketpair(), sysopen(),
> socket(), and accept()) now autovivify a file or directory handle if the
> handle passed to them is an uninitialized scalar variable. This allows
> the constructs such as open(my $fh, ...) and open(local $fh,...) to be
> used to create filehandles that will conveniently be closed
> automatically when the scope ends, provided there are no other
> references to them. This largely eliminates the need for typeglobs when
> opening filehandles that must be passed around, as in the following
> example:
>
>   sub myopen {
> open my $fh, "@_"
>  or die "Can't open '@_': $!";
> return $fh;
> }
>
> {
> my $f = myopen(" print <$f>;
> # $f implicitly closed here
> }
>
> 
>
> 5.6.0 also added 3-arg open($fh, $mode, $filename) for better safety
> against "injection" etc.
>
> Which means 5.5.x was the version that couldn't.
>
>
> -=- Bill
>
> Not speaking for the Firm.
>
>
>
> ___
> Boston-pm mailing list
> Boston-pm@mail.pm.org
> http://mail.pm.org/mailman/listinfo/boston-pm
>
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] version of perl that can use scalar filehandles

2006-05-23 Thread Greg London
5.6?
 
(weeps)
 
well, that'll never happen.
 
I'll have to recode with *GLOBS.
 
(weeps some more)
 
Thanks for all the replies.
 
Greg



From: Ricker, William [mailto:[EMAIL PROTECTED]
Sent: Tue 5/23/2006 4:23 PM
To: Greg London
Cc: boston-pm@mail.pm.org
Subject: RE: [Boston.pm] version of perl that can use scalar filehandles



> more importantly, what is the syntax for passing a filehandle
> into a routine if it is FILEHANDLE instead of $FILEHANDLE?

   open(FILEHANDLE, ">>$filename" ) or die "trying $!";


> open(my $fh, "filename");

Autovivification of unitialized scalar filehandles was added in 5.6.0
   http://search.cpan.org/~nwclark/perl-5.8.8/pod/perl56delta.pod


File and directory handles can be autovivified

Similar to how constructs such as $x->[0] autovivify a reference, handle
constructors (open(), opendir(), pipe(), socketpair(), sysopen(),
socket(), and accept()) now autovivify a file or directory handle if the
handle passed to them is an uninitialized scalar variable. This allows
the constructs such as open(my $fh, ...) and open(local $fh,...) to be
used to create filehandles that will conveniently be closed
automatically when the scope ends, provided there are no other
references to them. This largely eliminates the need for typeglobs when
opening filehandles that must be passed around, as in the following
example:

  sub myopen {
open my $fh, "@_"
 or die "Can't open '@_': $!";
return $fh;
}

{
my $f = myopen(";
# $f implicitly closed here
}



5.6.0 also added 3-arg open($fh, $mode, $filename) for better safety
against "injection" etc.

Which means 5.5.x was the version that couldn't.


-=- Bill

Not speaking for the Firm.


 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] version of perl that can use scalar filehandles

2006-05-23 Thread Ben Tilly
Use scalar filehandles?  You're probably thinking of 5.6.0, which was
the first that would let you autovivify filehandles.  As far as I
know, through the 5.x series you could always do:

  my $fh = do {local *fh};
  open($fh, "< $somefile") or die "Can't read '$somefile': $!";

If you didn't remember the do local trick, you could always use Symbol
and then call gensym to get your typeglob.

As for passing old-style filehandles, both of the following syntaxes
are likely to work:

  call_function(*FILEHANDLE);
  call_function(\*FILEHANDLE);

Cheers,
Ben

On 5/23/06, Greg London <[EMAIL PROTECTED]> wrote:
> more importantly, what is the syntax for passing a filehandle
> into a routine if it is FILEHANDLE instead of $FILEHANDLE?
>
>
> 
>
> From: [EMAIL PROTECTED] on behalf of Greg London
> Sent: Tue 5/23/2006 4:10 PM
> To: boston-pm@mail.pm.org
> Subject: [Boston.pm] version of perl that can use scalar filehandles
>
>
>
> what was the earliest version of perl that would
> allow you to use scalar filehandles?
>
> open(my $fh, "filename");
>
> instead of
>
> open(FILEHANDLE, "filename");
>
>
> ___
> Boston-pm mailing list
> Boston-pm@mail.pm.org
> http://mail.pm.org/mailman/listinfo/boston-pm
>
>
>
> ___
> Boston-pm mailing list
> Boston-pm@mail.pm.org
> http://mail.pm.org/mailman/listinfo/boston-pm
>
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] version of perl that can use scalar filehandles

2006-05-23 Thread Ron Newman
>more importantly, what is the syntax for passing a filehandle 
>into a routine if it is FILEHANDLE instead of $FILEHANDLE?

foo(*FILEHANDLE)
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] version of perl that can use scalar filehandles

2006-05-23 Thread Chris Ball
>> On Tue, 23 May 2006 16:10:40 -0400, Greg London said:

   > what was the earliest version of perl that would allow you to use
   > scalar filehandles?
 
5.6; see perldoc perl56delta, "File and directory handles can be
autovivified".

   > more importantly, what is the syntax for passing a filehandle 
   > into a routine if it is FILEHANDLE instead of $FILEHANDLE?

perldoc -q 'pass filehandles'.

- Chris.
-- 
Chris Ball   <[EMAIL PROTECTED]>

 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] version of perl that can use scalar filehandles

2006-05-23 Thread Ricker, William
> more importantly, what is the syntax for passing a filehandle 
> into a routine if it is FILEHANDLE instead of $FILEHANDLE?

   open(FILEHANDLE, ">>$filename" ) or die "trying $!";


> open(my $fh, "filename");

Autovivification of unitialized scalar filehandles was added in 5.6.0
   http://search.cpan.org/~nwclark/perl-5.8.8/pod/perl56delta.pod


File and directory handles can be autovivified

Similar to how constructs such as $x->[0] autovivify a reference, handle
constructors (open(), opendir(), pipe(), socketpair(), sysopen(),
socket(), and accept()) now autovivify a file or directory handle if the
handle passed to them is an uninitialized scalar variable. This allows
the constructs such as open(my $fh, ...) and open(local $fh,...) to be
used to create filehandles that will conveniently be closed
automatically when the scope ends, provided there are no other
references to them. This largely eliminates the need for typeglobs when
opening filehandles that must be passed around, as in the following
example:

  sub myopen {
open my $fh, "@_"
 or die "Can't open '@_': $!";
return $fh;
}

{
my $f = myopen(";
# $f implicitly closed here
} 



5.6.0 also added 3-arg open($fh, $mode, $filename) for better safety
against "injection" etc.

Which means 5.5.x was the version that couldn't.


-=- Bill

Not speaking for the Firm.
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] version of perl that can use scalar filehandles

2006-05-23 Thread Greg London
more importantly, what is the syntax for passing a filehandle 
into a routine if it is FILEHANDLE instead of $FILEHANDLE?
 



From: [EMAIL PROTECTED] on behalf of Greg London
Sent: Tue 5/23/2006 4:10 PM
To: boston-pm@mail.pm.org
Subject: [Boston.pm] version of perl that can use scalar filehandles



what was the earliest version of perl that would
allow you to use scalar filehandles?

open(my $fh, "filename");

instead of

open(FILEHANDLE, "filename");


___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


[Boston.pm] version of perl that can use scalar filehandles

2006-05-23 Thread Greg London
what was the earliest version of perl that would
allow you to use scalar filehandles?
 
open(my $fh, "filename");
 
instead of 
 
open(FILEHANDLE, "filename");
 
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm