[31615] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2874 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Mar 13 18:09:29 2010

Date: Sat, 13 Mar 2010 15:09:11 -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           Sat, 13 Mar 2010     Volume: 11 Number: 2874

Today's topics:
    Re: Asynchronous TCP Socket Connect <m@rtij.nl.invlalid>
    Re: open and read text file <ben@morrow.me.uk>
    Re: open and read text file <john@castleamber.com>
    Re: open and read text file <ben@morrow.me.uk>
    Re: open and read text file <uri@StemSystems.com>
    Re: open and read text file <ben@morrow.me.uk>
    Re: open and read text file <ben@morrow.me.uk>
    Re: open and read text file <john@castleamber.com>
    Re: open and read text file <ben@morrow.me.uk>
    Re: open and read text file <john@castleamber.com>
        Renaming an Uploaded Picture File <courses@shaw.ca>
    Re: Renaming an Uploaded Picture File <jurgenex@hotmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 13 Mar 2010 14:09:03 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: Asynchronous TCP Socket Connect
Message-Id: <fcfs67-cj6.ln1@news.rtij.nl>

On Fri, 12 Mar 2010 14:39:10 -0800, Jim Gibson wrote:

>> 
>> 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. :(

If you know the concept, the Perl solution is obvious, as Perl just 
exposes the same API. Answering how to do it in perl would be a 
disservice, as this is a complex subject, which is best learned from a 
book or some other source, not a perl newsgroup.

Rhe short answer is to set all your sockets to non-blocking, then use a 
select() loop to determine which handle is ready.

M4


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

Date: Sat, 13 Mar 2010 02:38:58 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: open and read text file
Message-Id: <2far67-5vk2.ln1@osiris.mauzo.dyndns.org>


Quoth John Bokma <john@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".

I think I'll take this opportunity to recommend 'autodie' again.

    ~% perl -Mautodie -e'open my $X, "<", "/not/there"'
    Can't open '/not/there' for reading: 'No such file or directory' at
    -e line 1
    ~%

No thought required :).

Ben



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

Date: Fri, 12 Mar 2010 20:58:54 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: open and read text file
Message-Id: <87bpetlyfl.fsf@castleamber.com>

Ben Morrow <ben@morrow.me.uk> writes:

> Quoth John Bokma <john@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".
>
> I think I'll take this opportunity to recommend 'autodie' again.
>
>     ~% perl -Mautodie -e'open my $X, "<", "/not/there"'
>     Can't open '/not/there' for reading: 'No such file or directory' at
>     -e line 1
>     ~%
>
> No thought required :).

Yup, I am aware of autodie I just have to start using it. Any chance it
will be a core module in the near feature (as it replaces Fatal)?

-- 
John Bokma                                                               j3b

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


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

Date: Sat, 13 Mar 2010 02:50:42 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: open and read text file
Message-Id: <25br67-5vk2.ln1@osiris.mauzo.dyndns.org>


Quoth jt@toerring.de (Jens Thoms Toerring):
> 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;-)

Mine too, but it's still in the documentation and I have the sort of
memory that keeps details like that while losing things like 'what I'm
doing next week' :).

> > 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!

Sorry, that was badly put. What I meant was:

    In the simplest case, you pass open an already-existing filehandle
    as its first argument:

        open STDIN, "<", "foo"

    However, if you pass it a glob that doesn't have a filehandle in it
    yet, or a scalar variable containing undef, it will create a new
    filehandle for you and either put it in the glob or put a ref to it
    in the variable.

(It's actually slightly more complicated in the 'variable' case: Perl
doesn't just create a filehandle, it also creates a glob (with a made-up
name) to put the filehandle in. I'm not entirely sure why it doesn't
just put an IO ref in the variable instead.)

Ben



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

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

>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:

  BM> Quoth jt@toerring.de (Jens Thoms Toerring):
  >> 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;-)

  BM> Mine too, but it's still in the documentation and I have the sort of
  BM> memory that keeps details like that while losing things like 'what I'm
  BM> doing next week' :).

  >> > 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!

  BM> Sorry, that was badly put. What I meant was:

  BM>     In the simplest case, you pass open an already-existing filehandle
  BM>     as its first argument:

  BM>         open STDIN, "<", "foo"

  BM>     However, if you pass it a glob that doesn't have a filehandle in it
  BM>     yet, or a scalar variable containing undef, it will create a new
  BM>     filehandle for you and either put it in the glob or put a ref to it
  BM>     in the variable.

  BM> (It's actually slightly more complicated in the 'variable' case: Perl
  BM> doesn't just create a filehandle, it also creates a glob (with a made-up
  BM> name) to put the filehandle in. I'm not entirely sure why it doesn't
  BM> just put an IO ref in the variable instead.)

well, underneath that io ref is a glob! why add extra layers you don't
need?

as for the made up glob name, it actually would be an anon glob which is
what the Symbol module does. it allocates a named symbol but lets it
leave scope and returns a ref to it. now the glob can't be accessed
except by the ref. details to be found in that module. the whole concept
of typeglobs and handles is a mess in perl anyhow.

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: Sat, 13 Mar 2010 14:12:11 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: open and read text file
Message-Id: <r2js67-qbs2.ln1@osiris.mauzo.dyndns.org>


Quoth John Bokma <john@castleamber.com>:
> Ben Morrow <ben@morrow.me.uk> writes:
> 
> > Quoth John Bokma <john@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".
> >
> > I think I'll take this opportunity to recommend 'autodie' again.
> >
> >     ~% perl -Mautodie -e'open my $X, "<", "/not/there"'
> >     Can't open '/not/there' for reading: 'No such file or directory' at
> >     -e line 1
> >     ~%
> >
> > No thought required :).
> 
> Yup, I am aware of autodie I just have to start using it. Any chance it
> will be a core module in the near feature (as it replaces Fatal)?

    ~% corelist autodie

    autodie was first released with perl 5.010001
    ~%

Ben



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

Date: Sat, 13 Mar 2010 14:20:43 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: open and read text file
Message-Id: <rijs67-qbs2.ln1@osiris.mauzo.dyndns.org>


Quoth "Uri Guttman" <uri@StemSystems.com>:
> >>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
> 
>   BM> (It's actually slightly more complicated in the 'variable' case: Perl
>   BM> doesn't just create a filehandle, it also creates a glob (with a made-up
>   BM> name) to put the filehandle in. I'm not entirely sure why it doesn't
>   BM> just put an IO ref in the variable instead.)
> 
> well, underneath that io ref is a glob! why add extra layers you don't
> need?

No, the glob is the extra layer you don't need. What perl gives you is a
ref to a glob containing only an IO, but it would be perfectly possible
to give you just a ref to an IO. It's the difference between \*FOO and
*FOO{IO}, or between Symbol::gensym and ::geniosym.

> as for the made up glob name, it actually would be an anon glob which is
> what the Symbol module does. it allocates a named symbol but lets it
> leave scope and returns a ref to it. now the glob can't be accessed
> except by the ref. details to be found in that module. the whole concept
> of typeglobs and handles is a mess in perl anyhow.

Yes.

Ben



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

Date: Sat, 13 Mar 2010 09:49:18 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: open and read text file
Message-Id: <87y6hwte69.fsf@castleamber.com>

Ben Morrow <ben@morrow.me.uk> writes:

> Quoth John Bokma <john@castleamber.com>:
>> Ben Morrow <ben@morrow.me.uk> writes:
>> 
>> > Quoth John Bokma <john@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".
>> >
>> > I think I'll take this opportunity to recommend 'autodie' again.
>> >
>> >     ~% perl -Mautodie -e'open my $X, "<", "/not/there"'
>> >     Can't open '/not/there' for reading: 'No such file or directory' at
>> >     -e line 1
>> >     ~%
>> >
>> > No thought required :).
>> 
>> Yup, I am aware of autodie I just have to start using it. Any chance it
>> will be a core module in the near feature (as it replaces Fatal)?
>
>     ~% corelist autodie
>
>     autodie was first released with perl 5.010001
>     ~%
>
> Ben

john@ecce:~$ perl -Mautodie -e1
Can't locate autodie.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .).
BEGIN failed--compilation aborted.
john@ecce:~$ corelist autodie

autodie was not in CORE (or so I think)
john@ecce:~$ 

-- 
John Bokma                                                               j3b

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


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

Date: Sat, 13 Mar 2010 16:13:24 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: open and read text file
Message-Id: <46qs67-t1u2.ln1@osiris.mauzo.dyndns.org>


Quoth John Bokma <john@castleamber.com>:
> Ben Morrow <ben@morrow.me.uk> writes:
> 
> > Quoth John Bokma <john@castleamber.com>:
> >> 
> >> Yup, I am aware of autodie I just have to start using it. Any chance it
> >> will be a core module in the near feature (as it replaces Fatal)?
> >
> >     ~% corelist autodie
> >
> >     autodie was first released with perl 5.010001
                                             ^^^^^^^^
>
> john@ecce:~$ perl -Mautodie -e1
> Can't locate autodie.pm in @INC (@INC contains: /etc/perl
> /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5
                      ^^^^^^
> /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10
> /usr/local/lib/site_perl .).
> BEGIN failed--compilation aborted.
> john@ecce:~$ corelist autodie
> 
> autodie was not in CORE (or so I think)
> john@ecce:~$ 

The version of Module::CoreList shipped with 5.10.0 only knows about
modules that went in before 5.10.0 shipped. You need 5.10.1.

Ben



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

Date: Sat, 13 Mar 2010 10:24:13 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: open and read text file
Message-Id: <87pr38tck2.fsf@castleamber.com>

Ben Morrow <ben@morrow.me.uk> writes:

> Quoth John Bokma <john@castleamber.com>:
>> Ben Morrow <ben@morrow.me.uk> writes:
>> 
>> > Quoth John Bokma <john@castleamber.com>:
>> >> 
>> >> Yup, I am aware of autodie I just have to start using it. Any chance it
>> >> will be a core module in the near feature (as it replaces Fatal)?
>> >
>> >     ~% corelist autodie
>> >
>> >     autodie was first released with perl 5.010001
>                                              ^^^^^^^^
>>
>> john@ecce:~$ perl -Mautodie -e1
>> Can't locate autodie.pm in @INC (@INC contains: /etc/perl
>> /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5
>                       ^^^^^^
>> /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10
>> /usr/local/lib/site_perl .).
>> BEGIN failed--compilation aborted.
>> john@ecce:~$ corelist autodie
>> 
>> autodie was not in CORE (or so I think)
>> john@ecce:~$ 
>
> The version of Module::CoreList shipped with 5.10.0 only knows about
> modules that went in before 5.10.0 shipped. You need 5.10.1.

Ah, shoot. I read 5.010001 as a very, very old Perl 5.0000x version, not
as 5.10.1

Anyway, thanks. I want to switch to autodie in all my (new) scripts for
some time, and have now no more excuses not to.

-- 
John Bokma                                                               j3b

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


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

Date: Sat, 13 Mar 2010 11:54:25 -0800
From: "Doug H" <courses@shaw.ca>
Subject: Renaming an Uploaded Picture File
Message-Id: <53Smn.32025$sx5.23766@newsfe16.iad>

I am a fairly new Perl programmer so am hoping that my problem is just a 
simple
mistake that someone can easily help me with.

I have a short Perl script that gets some information from a form on a web 
page
and then uploads a picture file to the web site. This part works fine. My 
problem
occurs when I try to rename the file that was just uploaded. My coding is as
follows:

#!/usr/local/bin/perl -wT
use CGI;
#get info about file to upload
$upload_dir = "/d4/d8/pscc.shawbiz.ca/html/";
$query = new CGI;  $filename = $query->param("uploadfile");
#get other information to process
$picnum= $query->param("picnum");
$pcomm= $query->param("pcomm");
$wpage= $query->param("wpage");
#do the uploading
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle = $query->upload("uploadfile");
open UPLOADFILE, ">$upload_dir/$filename";
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}
close UPLOADFILE;
#rename the uploaded file
my $newfilename="/d4/d8/pscc.shawbiz.ca/html/bunnysredone.jpg";
rename ("/d4/d8/pscc.shawbiz.ca/html/bunnys.jpg",$newfilename);
 ....
etc (the rest just shows a web page showing the results)

In the above the renaming of the uploaded file works. It renames
the picture file from 'bunnys.jpg' to 'bunnysredone.jpg'. What I
need, though, is to have the new picture name created from
the picture number that was uploaded (the value of $picnum). So
I replaced the line for $newfilename to be

my $newfilename="/d4/d8/pscc.shawbiz.ca/html/pic".$picnum.".jpg";

With this change, the renaming no longer works. If I comment out
the actual rename line of code and display the value of $newfilename
in the output, it shows exactly what I would have expected it to
be
  eg
      /d4/d8/pscc.shawbiz.ca/html/pic5.jpg

where the 5 represents the current value for $picnum.

I do not see what I am doing wrong. Can anyone help?  When I get
this working I would also like to use the same technique to set
the value of the old name in the rename line.

Thank you.




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

Date: Sat, 13 Mar 2010 12:39:23 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Renaming an Uploaded Picture File
Message-Id: <bftnp51n2kcec7loik2qdktlhsdfntiuom@4ax.com>

"Doug H" <courses@shaw.ca> wrote:
[...]
>my $newfilename="/d4/d8/pscc.shawbiz.ca/html/bunnysredone.jpg";
>rename ("/d4/d8/pscc.shawbiz.ca/html/bunnys.jpg",$newfilename);
>....
>In the above the renaming of the uploaded file works. It renames
>the picture file from 'bunnys.jpg' to 'bunnysredone.jpg'. 

Ok.

>What I
>need, though, is to have the new picture name created from
>the picture number that was uploaded (the value of $picnum). So
>I replaced the line for $newfilename to be
>
>my $newfilename="/d4/d8/pscc.shawbiz.ca/html/pic".$picnum.".jpg";

Yikes! This is Perl, not C. No need for string concatenation:
	my $newfilename="/d4/d8/pscc.shawbiz.ca/html/pic$picnum.jpg";

>With this change, the renaming no longer works. If I comment out

Why don't you ask perl to help you?
	
rename (....) or 
    die "Cannot rename file '$oldfilename' to '$newfilename': $!";

You may also want to re-check the documentation for rename(), as it does
have its OS-specific quirks. File::Copy might be a better solution.

jue


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

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 2874
***************************************


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