[31614] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 2873 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Mar 12 21:09:29 2010

Date: Fri, 12 Mar 2010 18:09:09 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 12 Mar 2010     Volume: 11 Number: 2873

Today's topics:
    Re: Asynchronous TCP Socket Connect <pshendley@gmail.com>
    Re: Asynchronous TCP Socket Connect <jimsgibson@gmail.com>
    Re: Asynchronous TCP Socket Connect <uri@StemSystems.com>
    Re: Asynchronous TCP Socket Connect <derykus@gmail.com>
    Re: FAQ 5.39 Why do I get weird spaces when I print an  <pshendley@gmail.com>
        open and read text file <ron.eggler@gmail.com>
    Re: open and read text file <john@castleamber.com>
    Re: open and read text file <m@rtij.nl.invlalid>
    Re: open and read text file <ron.eggler@gmail.com>
    Re: open and read text file (Jens Thoms Toerring)
    Re: open and read text file <ben@morrow.me.uk>
    Re: open and read text file (Jens Thoms Toerring)
    Re: open and read text file <john@castleamber.com>
    Re: open and read text file <uri@StemSystems.com>
    Re: open and read text file <jurgenex@hotmail.com>
    Re: worst concept in computer science (Pascal J. Bourguignon)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Fri, 12 Mar 2010 12:54:47 -0800 (PST)
From: Permostat <pshendley@gmail.com>
Subject: Re: Asynchronous TCP Socket Connect
Message-Id: <d9efc775-44eb-4556-a43e-cf73141e60f3@q15g2000yqj.googlegroups.com>

On Mar 12, 12:23=A0pm, Martijn Lievaart <m...@rtij.nl.invlalid> wrote:
> On Fri, 12 Mar 2010 10:10:25 -0700, Steve Roscio wrote:
> > Howdy -
>
> > Is there a way to do an asynchronous connection of a TCP socket, withou=
t
> > threads or forking? =A0Something like a two-step method, where I start =
the
> > connect, then later check to see if it's done and if it worked?
>
> > So basically I want to turn this:
>
> > =A0 =A0 =A0my $s =3D IO::Socket::INET->new(
> > =A0 =A0 =A0 =A0 =A0Proto =A0 =A0=3D> $proto,
> > =A0 =A0 =A0 =A0 =A0PeerAddr =3D> $addr,
> > =A0 =A0 =A0 =A0 =A0PeerPort =3D> $port,
> > =A0 =A0 =A0 =A0 =A0# =A0Blocking =3D> 0,
> > =A0 =A0 =A0 =A0 =A0Timeout =3D> $timeout,
> > =A0 =A0 =A0);
>
> > into a two-step call:
> > =A0 =A0 =A0my $s =3D start_connection (...);
>
> > =A0 =A0 =A0# later...
> > =A0 =A0 =A0if ($s->connected() || $s->error()) ... =A0#(whatever)
>
> This is not a Perl question, You are looking for non-blocking sockets.
> Whole volumes have been written about this, but best is Stevens "Unix
> Network Programming".
>
> HTH,
> M4

Yeah Steve. Derp bee derp imma TARD. -> YOU.

squirm-


------------------------------

Date: Fri, 12 Mar 2010 14:39:10 -0800
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: Asynchronous TCP Socket Connect
Message-Id: <120320101439100109%jimsgibson@gmail.com>

In article <cedq67-cj6.ln1@news.rtij.nl>, Martijn Lievaart
<m@rtij.nl.invlalid> wrote:

> On Fri, 12 Mar 2010 10:10:25 -0700, Steve Roscio wrote:
> 
> > Howdy -
> > 
> > Is there a way to do an asynchronous connection of a TCP socket, without
> > threads or forking?  Something like a two-step method, where I start the
> > connect, then later check to see if it's done and if it worked?
> > 
> > So basically I want to turn this:
> > 
> >      my $s = IO::Socket::INET->new(
> >          Proto    => $proto,
> >          PeerAddr => $addr,
> >          PeerPort => $port,
> >          #  Blocking => 0,
> >          Timeout => $timeout,
> >      );
> > 
> > into a two-step call:
> >      my $s = start_connection (...);
> > 
> >      # later...
> >      if ($s->connected() || $s->error()) ...  #(whatever)
> 
> This is not a Perl question, You are looking for non-blocking sockets. 
> Whole volumes have been written about this, but best is Stevens "Unix 
> Network Programming".

But asking how to do it in Perl _is_ a Perl question. Alas, I do not
know the answer. :(

The documentation to IO::Socket::INET indicates that setting 'Blocking
=> 0' in the arguments to IO::Socket::INET make the connect call
non-blocking. The documentation to IO::Socket describes the connected()
method that returns undef if the socket is not in a connected state.
Perhaps those two together, with suitable waiting intervals and
retries, provide the needed functionality.

-- 
Jim Gibson


------------------------------

Date: Fri, 12 Mar 2010 18:05:50 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Asynchronous TCP Socket Connect
Message-Id: <877hphkunl.fsf@quad.sysarch.com>

>>>>> "JG" == Jim Gibson <jimsgibson@gmail.com> writes:

  JG> In article <cedq67-cj6.ln1@news.rtij.nl>, Martijn Lievaart
  JG> <m@rtij.nl.invlalid> wrote:

  >> On Fri, 12 Mar 2010 10:10:25 -0700, Steve Roscio wrote:
  >> 
  >> > Howdy -
  >> > 
  >> > Is there a way to do an asynchronous connection of a TCP socket, without
  >> > threads or forking?  Something like a two-step method, where I start the
  >> > connect, then later check to see if it's done and if it worked?
  >> > 
  >> > So basically I want to turn this:
  >> > 
  >> >      my $s = IO::Socket::INET->new(
  >> >          Proto    => $proto,
  >> >          PeerAddr => $addr,
  >> >          PeerPort => $port,
  >> >          #  Blocking => 0,
  >> >          Timeout => $timeout,
  >> >      );
  >> > 
  >> > into a two-step call:
  >> >      my $s = start_connection (...);
  >> > 
  >> >      # later...
  >> >      if ($s->connected() || $s->error()) ...  #(whatever)
  >> 
  >> This is not a Perl question, You are looking for non-blocking sockets. 
  >> Whole volumes have been written about this, but best is Stevens "Unix 
  >> Network Programming".

  JG> But asking how to do it in Perl _is_ a Perl question. Alas, I do not
  JG> know the answer. :(

  JG> The documentation to IO::Socket::INET indicates that setting 'Blocking
  JG> => 0' in the arguments to IO::Socket::INET make the connect call
  JG> non-blocking. The documentation to IO::Socket describes the connected()
  JG> method that returns undef if the socket is not in a connected state.
  JG> Perhaps those two together, with suitable waiting intervals and
  JG> retries, provide the needed functionality.

even better is to use an event loop and you can tell when a socket is
connected by checking if it can be written. this is how event loops do
async socket connections while also handling other events. you do need
to set the socket to non-blocking before the connect call. and this will
not affect the block nature of the i/o so if you want async i/o too you
need to set non-blocking AFTER the connect happens.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


------------------------------

Date: Fri, 12 Mar 2010 17:17:02 -0800 (PST)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: Asynchronous TCP Socket Connect
Message-Id: <2e6a9274-2166-4d83-87dc-6f6e6cc02436@e19g2000prn.googlegroups.com>

On Mar 12, 9:10=A0am, Steve Roscio <Steve.Ros...@hp.com> wrote:
> Howdy -
>
> Is there a way to do an asynchronous connection of a TCP socket, without
> threads or forking? =A0Something like a two-step method, where I start th=
e
> connect, then later check to see if it's done and if it worked?
>
> So basically I want to turn this:
>
> =A0 =A0 =A0my $s =3D IO::Socket::INET->new(
> =A0 =A0 =A0 =A0 =A0Proto =A0 =A0=3D> $proto,
> =A0 =A0 =A0 =A0 =A0PeerAddr =3D> $addr,
> =A0 =A0 =A0 =A0 =A0PeerPort =3D> $port,
> =A0 =A0 =A0 =A0 =A0# =A0Blocking =3D> 0,
> =A0 =A0 =A0 =A0 =A0Timeout =3D> $timeout,
> =A0 =A0 =A0);
>
> into a two-step call:
> =A0 =A0 =A0my $s =3D start_connection (...);
>
> =A0 =A0 =A0# later...
> =A0 =A0 =A0if ($s->connected() || $s->error()) ... =A0#(whatever)
>

Here's a (slightly modified) strategy from
"Network Programming in Perl" (L.Stein):


   > my $s =3DIO::Socket::INET->new( ..Blocking=3D>0 )
     ...

   use Errno qw(EWOULDBLOCK);
     ...
   my $rc =3D sysread( $s, $data, $bytes );
   if ( defined $rc )
   {
       if ( $rc > 0 ){ # success
          ...
       else           { # EOF
          ...
       }
   }
   elsif ( $! =3D=3D EWOULDBLOCK )
   {
        # retry code
   }
   else
   {
        # unexpected error .. examine $!
   }

--
Charles DeRykus


------------------------------

Date: Fri, 12 Mar 2010 12:56:12 -0800 (PST)
From: Permostat <pshendley@gmail.com>
Subject: Re: FAQ 5.39 Why do I get weird spaces when I print an array of  lines?
Message-Id: <24910be0-a33a-4c11-9341-75ea841ca950@o30g2000yqb.googlegroups.com>

On Mar 12, 11:00=A0am, PerlFAQ Server <br...@theperlreview.com> wrote:
> This is an excerpt from the latest version perlfaq5.pod, which
> comes with the standard Perl distribution. These postings aim to
> reduce the number of repeated questions as well as allow the community
> to review and update the answers. The latest version of the complete
> perlfaq is athttp://faq.perl.org.
>
> --------------------------------------------------------------------
>
> 5.39: Why do I get weird spaces when I print an array of lines?
>
> =A0 =A0 (contributed by brian d foy)
>
> =A0 =A0 If you are seeing spaces between the elements of your array when =
you
> =A0 =A0 print the array, you are probably interpolating the array in doub=
le
> =A0 =A0 quotes:
>
> =A0 =A0 =A0 =A0 =A0 =A0 my @animals =3D qw(camel llama alpaca vicuna);
> =A0 =A0 =A0 =A0 =A0 =A0 print "animals are: @animals\n";
>
> =A0 =A0 It's the double quotes, not the "print", doing this. Whenever you
> =A0 =A0 interpolate an array in a double quote context, Perl joins the el=
ements
> =A0 =A0 with spaces (or whatever is in $", which is a space by default):
>
> =A0 =A0 =A0 =A0 =A0 =A0 animals are: camel llama alpaca vicuna
>
> =A0 =A0 This is different than printing the array without the interpolati=
on:
>
> =A0 =A0 =A0 =A0 =A0 =A0 my @animals =3D qw(camel llama alpaca vicuna);
> =A0 =A0 =A0 =A0 =A0 =A0 print "animals are: ", @animals, "\n";
>
> =A0 =A0 Now the output doesn't have the spaces between the elements becau=
se the
> =A0 =A0 elements of @animals simply become part of the list to "print":
>
> =A0 =A0 =A0 =A0 =A0 =A0 animals are: camelllamaalpacavicuna
>
> =A0 =A0 You might notice this when each of the elements of @array end wit=
h a
> =A0 =A0 newline. You expect to print one element per line, but notice tha=
t every
> =A0 =A0 line after the first is indented:
>
> =A0 =A0 =A0 =A0 =A0 =A0 this is a lion
> =A0 =A0 =A0 =A0 =A0 =A0 =A0this is another lion
> =A0 =A0 =A0 =A0 =A0 =A0 =A0this is a porcupine
>
> =A0 =A0 That extra space comes from the interpolation of the array. If yo=
u don't
> =A0 =A0 want to put anything between your array elements, don't use the a=
rray in
> =A0 =A0 double quotes. You can send it to print without them:
>
> =A0 =A0 =A0 =A0 =A0 =A0 print @lines;
>
> --------------------------------------------------------------------
>
> The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
> are not necessarily experts in every domain where Perl might show up,
> so please include as much information as possible and relevant in any
> corrections. The perlfaq-workers also don't have access to every
> operating system or platform, so please include relevant details for
> corrections to examples that do not work on particular platforms.
> Working code is greatly appreciated.
>
> If you'd like to help maintain the perlfaq, see the details in
> perlfaq.pod.

Fix'd

squirm-


------------------------------

Date: Fri, 12 Mar 2010 13:40:47 -0800 (PST)
From: cerr <ron.eggler@gmail.com>
Subject: open and read text file
Message-Id: <4c7fbc78-4731-46da-bdaf-85b8c50e474a@w27g2000pre.googlegroups.com>

Hi There,

I'm just trying to open a text file and print line by line.
My Code:
	my $HANDLE = $filename;
        open(HANDLE) or die("Could not open GPS source file.");

        foreach $line (<HANDLE>) {
	  print $line;
	  print $client $line;

	  sleep(1);
        }
	close(HANDLE);
My Problem:
The script keeps dying on open(). :(
The pernmissions of the file are set to -rw-r--r--
So what is the problem here? Not getting it...

Thanks,
Ron


------------------------------

Date: Fri, 12 Mar 2010 16:15:09 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: open and read text file
Message-Id: <877hphp4pe.fsf@castleamber.com>

cerr <ron.eggler@gmail.com> writes:

> Hi There,
>
> I'm just trying to open a text file and print line by line.
> My Code:
> 	my $HANDLE = $filename;
>         open(HANDLE) or die("Could not open GPS source file.");

open my $fh, '<', $filename 
    or die "Can't open '$filename' for reading: $!";
                  ^^^^  ^^^^^^^^^      ^^^^^^^  ^^

Make sure to always add all four items marked with ^


>         foreach $line (<HANDLE>) {
> 	  print $line;
> 	  print $client $line;

while ( my $line = <$fh> ) {

     print $line;
     print $client $line;

>
> 	  sleep(1);

why?

>         }
> 	close(HANDLE);

I also check close, i.e.

  close $fh or die "Can't close '$filename' after reading: $!";
                          ^^^^^  ^^^^^^^^^        ^^^^^^^  ^^
                           
Notice again those four items.

-- 
John Bokma                                                               j3b

Hacking & Hiking in Mexico -  http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development


------------------------------

Date: Fri, 12 Mar 2010 23:23:15 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: open and read text file
Message-Id: <ifrq67-cj6.ln1@news.rtij.nl>

On Fri, 12 Mar 2010 13:40:47 -0800, cerr wrote:

> Hi There,
> 
> I'm just trying to open a text file and print line by line. My Code:
> 	my $HANDLE = $filename;
>         open(HANDLE) or die("Could not open GPS source file.");

my $handle;
open($handle, "<", $filename) or die("Could not open GPS source file: 
$!");
> 
>         foreach $line (<HANDLE>) {
for my $line (<$handle) {

> 	  print $line;
> 	  print $client $line;
> 
> 	  sleep(1);

Why the sleep?????



HTH,
M4


------------------------------

Date: Fri, 12 Mar 2010 14:29:28 -0800 (PST)
From: cerr <ron.eggler@gmail.com>
Subject: Re: open and read text file
Message-Id: <a838075c-811d-4536-8778-24cdc8e0e701@s36g2000prf.googlegroups.com>

On Mar 12, 2:23=A0pm, Martijn Lievaart <m...@rtij.nl.invlalid> wrote:
> On Fri, 12 Mar 2010 13:40:47 -0800, cerr wrote:
> > Hi There,
>
> > I'm just trying to open a text file and print line by line. My Code:
> > =A0 =A0my $HANDLE =3D $filename;
> > =A0 =A0 =A0 =A0 open(HANDLE) or die("Could not open GPS source file.");
>
> my $handle;
> open($handle, "<", $filename) or die("Could not open GPS source file:
> $!");
>
> > =A0 =A0 =A0 =A0 foreach $line (<HANDLE>) {
>
> for my $line (<$handle) {
>
> > =A0 =A0 =A0print $line;
> > =A0 =A0 =A0print $client $line;
>
> > =A0 =A0 =A0sleep(1);
>
> Why the sleep?????

Because i wannt print one line per second only ;)

Thanks Martjin and John, I got it going now!!!

--
cerr


------------------------------

Date: 12 Mar 2010 22:45:21 GMT
From: jt@toerring.de (Jens Thoms Toerring)
Subject: Re: open and read text file
Message-Id: <7vvug1FlujU1@mid.uni-berlin.de>

cerr <ron.eggler@gmail.com> wrote:
> I'm just trying to open a text file and print line by line.
> My Code:
>         my $HANDLE = $filename;
>         open(HANDLE) or die("Could not open GPS source file.");

>         foreach $line (<HANDLE>) {
>           print $line;
>           print $client $line;
>
>           sleep(1);
>         }
>         close(HANDLE);
> My Problem:
> The script keeps dying on open(). :(
> The pernmissions of the file are set to -rw-r--r--
> So what is the problem here? Not getting it...

Because your use of open() is completely broken. No kind of
open() function I have ever seen (as far as I remember) works
like that. All take a file name and return a handle that in
subsequent calls of functions to read from or write to the
file etc. is used. A handle is something very different from
the file's name (with the UNIX open() function it's an integer,
with C's fopen() function it's a pointer to some structure)
- and in Perl it's something you don't need to worry about;-)
And open() functions typically also take at least one more
argument that tells for what purpose the file is to be opened
for (i.e. for reading, writing or both - that makes a lot of a
difference when you have e.g. permission to read from but not
to write to the file) etc. And sometimes even additional flags
can or have to be given.

If you want a file opened for reading do

open my $handle, '<', $filename or die "Can't open file\n";
while ( my $line = <$handle> ) { ... }
close $handle;

The first argument to open() is the variable that, after a
successful call of open(), contains the handle for the file.
The second, '<', tells that you want it to be opened for
reading (use '>' for writing, but there are a number of
further possibilities, see 'perldoc -f open' for all the
gory details). And the third is the file name (or, with
other second arguments, it could be a program that then
gets run and from which you want to read its output or to
which you want to send data).

BTW, if I remember correctly, the difference between

for my $line ( <> ) { ... }

and

while ( my $line = <> ) { ... }

is that with 'for' the '<>' is used in list context, so the
whole file has to be read in immediately and thus stored in
memory as a whole. This normally doesn't make much sense.
With 'while' it gets read in line by line instead, so only
a single line has to be kept in memory.

                               Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de


------------------------------

Date: Fri, 12 Mar 2010 23:00:04 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: open and read text file
Message-Id: <kktq67-h7j2.ln1@osiris.mauzo.dyndns.org>


Quoth jt@toerring.de (Jens Thoms Toerring):
> cerr <ron.eggler@gmail.com> wrote:
> > I'm just trying to open a text file and print line by line.
> > My Code:
> >         my $HANDLE = $filename;
> >         open(HANDLE) or die("Could not open GPS source file.");
> 
> Because your use of open() is completely broken. No kind of
> open() function I have ever seen (as far as I remember) works
> like that.

It's a Perl 4ism that is never used nowadays, but still documented and
supported. A bare

    open HANDLE;

takes the filename from the global $HANDLE in the current package. In
the OP's case it's failing because he sets a lexical $HANDLE instead of
a global.

> All take a file name and return a handle that in
> subsequent calls of functions to read from or write to the
> file etc. is used.

open doesn't return a filehandle (though some things would be more
convenient if it did). It opens an existing filehandle, auto-vivifying
it if necessary. (It is perfectly OK to pass an already-open filehandle
to open, and it will close it first.)

Ben



------------------------------

Date: 12 Mar 2010 23:23:29 GMT
From: jt@toerring.de (Jens Thoms Toerring)
Subject: Re: open and read text file
Message-Id: <8000nhF83sU1@mid.uni-berlin.de>

Ben Morrow <ben@morrow.me.uk> wrote:
> Quoth jt@toerring.de (Jens Thoms Toerring):
> > cerr <ron.eggler@gmail.com> wrote:
> > > I'm just trying to open a text file and print line by line.
> > > My Code:
> > >         my $HANDLE = $filename;
> > >         open(HANDLE) or die("Could not open GPS source file.");
> > 
> > Because your use of open() is completely broken. No kind of
> > open() function I have ever seen (as far as I remember) works
> > like that.

> It's a Perl 4ism that is never used nowadays, but still documented and
> supported. A bare

>     open HANDLE;

Uuups, that's from before my time with Perl. And do I feel lucky;-)

> takes the filename from the global $HANDLE in the current package. In
> the OP's case it's failing because he sets a lexical $HANDLE instead of
> a global.

> > All take a file name and return a handle that in
> > subsequent calls of functions to read from or write to the
> > file etc. is used.

> open doesn't return a filehandle (though some things would be more
> convenient if it did). It opens an existing filehandle, auto-vivifying
> it if necessary. (It is perfectly OK to pass an already-open filehandle
> to open, and it will close it first.)

Mmmm, what means auto-vivifying in this context? And what "opens an
existing file handle"? Obviously, not understanding enough I just see
a variable, not an "existing file handle". Since I can't remember you
being wrong on such things I guess there's some kind of magic going on
I didn't grok yet, so a bit more of explanation would be great!

And I also didn't know about a file getting closed automatically when
the file handle is re-used. What happens when you do e.g.

open my $h1, '<', 'filename1.txt';
my $h2 = $h1;
open $h1, '<', 'filename2.txt';

Does that close the first file or does it stay open because of
'$h2' still refering to it?
                                 Best regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de


------------------------------

Date: Fri, 12 Mar 2010 17:35:01 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: open and read text file
Message-Id: <87vdd1nmfu.fsf@castleamber.com>

jt@toerring.de (Jens Thoms Toerring) writes:

> open my $handle, '<', $filename or die "Can't open file\n";

To me the acceptable minimum for the rhs of or would be:

       die "Can't open '$filename': $!";

this reports and the filename and why it couldn't be opened.


Personally I prefer to add "for reading" since I consider it more clear
compared to just "Can't open".

-- 
John Bokma                                                               j3b

Hacking & Hiking in Mexico -  http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development


------------------------------

Date: Fri, 12 Mar 2010 19:02:11 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: open and read text file
Message-Id: <87ljdxjdh8.fsf@quad.sysarch.com>

>>>>> "JTT" == Jens Thoms Toerring <jt@toerring.de> writes:

  >> It's a Perl 4ism that is never used nowadays, but still documented and
  >> supported. A bare

  >> open HANDLE;

  JTT> Uuups, that's from before my time with Perl. And do I feel lucky;-)

and it still is supported. nasty old concept. it is used in golfing
sometimes.

  >> open doesn't return a filehandle (though some things would be more
  >> convenient if it did). It opens an existing filehandle, auto-vivifying
  >> it if necessary. (It is perfectly OK to pass an already-open filehandle
  >> to open, and it will close it first.)

  JTT> Mmmm, what means auto-vivifying in this context? And what "opens an
  JTT> existing file handle"? Obviously, not understanding enough I just see
  JTT> a variable, not an "existing file handle". Since I can't remember you
  JTT> being wrong on such things I guess there's some kind of magic going on
  JTT> I didn't grok yet, so a bit more of explanation would be great!

open my $handle will autovivify a handle in the scalar if it was
undef. same as $ref->{foo} will do if $ref is undef. it used to be you
had to have a typeglob or ref to one in the variable so open would
work. the better lazy way is for perl to do it for you.

  JTT> And I also didn't know about a file getting closed automatically when
  JTT> the file handle is re-used. What happens when you do e.g.

  JTT> open my $h1, '<', 'filename1.txt';
  JTT> my $h2 = $h1;
  JTT> open $h1, '<', 'filename2.txt';

  JTT> Does that close the first file or does it stay open because of
  JTT> '$h2' still refering to it?

the handle isn't really in the scalar, but is refered to by the scalar
(the handle is actually a typeglob entry). the file itself will be
closed as will the handle itself. the extra copy now refers to a closed
file and will spit out an error if used for i/o.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


------------------------------

Date: Fri, 12 Mar 2010 16:07:56 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: open and read text file
Message-Id: <8hllp5d4n4kaf21kju6rk5fb3vethm5r4e@4ax.com>

cerr <ron.eggler@gmail.com> wrote:
>I'm just trying to open a text file and print line by line.
>My Code:
>	my $HANDLE = $filename;
>        open(HANDLE) or die("Could not open GPS source file.");

This style is an anachronism from the dark ages, supported only for
backward compatibility of ancient code.

Today you should use the three-argument form of open() with a lexical
file handle
	open ($Handle, '<', $filename) 
and in the error message I would at least include the filename and the
reason why the error occured:
	    or die ("Couldn't open GPS source file '$filename': $!";)

jue


------------------------------

Date: Fri, 12 Mar 2010 20:39:10 +0100
From: pjb@informatimago.com (Pascal J. Bourguignon)
Subject: Re: worst concept in computer science
Message-Id: <87hbols529.fsf@galatea.lan.informatimago.com>

m_mommer@yahoo.com (Mario S. Mommer) writes:

> Nick Keighley <nick_keighley_nospam@hotmail.com> writes:
>> On 10 Mar, 20:39, Jürgen Exner <jurge...@hotmail.com> wrote:
>>
>>> Actually C pointers are probably among the worst concepts ever invented
>>> in computer science.
>>
>> now there's a challenge. Who are the other competitors?
>>
>> - FORTRAN computed goto
>> - FORTRAN fixed layout
>> - Algol-60 free layout
>> - Algol-60 call by name
>> - Pascal with
>> - C declaration syntax
>> - C++ template syntax
>> - C++ exception specification
>> - PL/I
>
> I miss
>
>  - FORTRAN common blocks
>
> from that list. And with respect to C++ templates - well, the syntax
> isn't really the worst part.



#include <iostream>

template <int N> struct Factorial {
  enum { value = N*Factorial<N-1>::value };
};

template <> struct Factorial<1> {
  enum { value = 1 };
};

int main()
  const int f15=Factorial<15>::value;
  std::cout << f15 << std::endl;
  return 0;
}


I lose track of the count of syntactic irregularities...
But you're right, it's not the worst part:


-*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Fri Mar 12 20:19:03

SRC="/tmp/f.c++" ; EXE="f" ; g++ -I. -L. -g3 -ggdb3 -o ${EXE} ${SRC} &&  ./${EXE} && echo status = $?
/tmp/f.c++: In instantiation of 'Factorial<13>':
/tmp/f.c++:4:   instantiated from 'Factorial<14>'
/tmp/f.c++:4:   instantiated from 'Factorial<15>'
/tmp/f.c++:12:   instantiated from here
/tmp/f.c++:4: warning: integer overflow in expression
/tmp/f.c++: In instantiation of 'Factorial<14>':
/tmp/f.c++:4:   instantiated from 'Factorial<15>'
/tmp/f.c++:12:   instantiated from here
/tmp/f.c++:4: warning: integer overflow in expression
/tmp/f.c++: In instantiation of 'Factorial<15>':
/tmp/f.c++:12:   instantiated from here
/tmp/f.c++:4: warning: integer overflow in expression
2004310016
status = 0

Compilation finished at Fri Mar 12 20:19:04




vs.


(defun fact (n) (if (= 1 n) 1 (* n (fact (- n 1)))))
(defmacro factorial (n) (fact n))
(defun main ()
   (let ((f15 (factorial 15)))
     (print f15)
     0))
(main)

1307674368000 
0


-- 
__Pascal Bourguignon__


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 2873
***************************************


home help back first fref pref prev next nref lref last post