[22060] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4282 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Dec 18 14:12:03 2002

Date: Wed, 18 Dec 2002 11:10:16 -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           Wed, 18 Dec 2002     Volume: 10 Number: 4282

Today's topics:
        What means sh: ^P: not found <stremitz@consultant.com>
    Re: Why do beginners read files into an array? (Helgi Briem)
    Re: Why do beginners read files into an array? <wsegrave@mindspring.com>
    Re: Why do beginners read files into an array? <joe+usenet@sunstarsys.com>
    Re: Why do beginners read files into an array? <jurgenex@hotmail.com>
    Re: Why do beginners read files into an array? ctcgag@hotmail.com
    Re: Why do beginners read files into an array? <jeff@vpservices.com>
    Re: Why do beginners read files into an array? <comdog@panix.com>
    Re: Why doesn't this foreach SMTP loop work? <Nick_Djurovich@NOSPAM.aimtechnology.com>
    Re: Why doesn't this foreach SMTP loop work? <ian@WINDOZEdigiserv.net>
        Why is system call exit code factor of 256? <bhoppe@ti.com>
    Re: Why is system call exit code factor of 256? (Walter Roberson)
    Re: Why is system call exit code factor of 256? <comdog@panix.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 18 Dec 2002 14:02:57 -0500
From: Alexander Stremitzer <stremitz@consultant.com>
Subject: What means sh: ^P: not found
Message-Id: <3E00C661.8010705@consultant.com>

I am trying to copy a file from one location to another utilizing the 
non aliased version of Solaris cp.

When doing the command in the shell itself, it works.
$ \cp xxx1 yyy1

When doing it through system in Perl, it fails. Does anybody know what 
goes on here ?

I know I could replace "\\\cp ./xxx1 ./yyy1" with "unalias cp; cp ./xxx1 
 ./yyy1" and make it work. But I am curious what is wrong here.

Thanks for your help,
Alex

pds:/db0_d/_alex/hup/pmc> cat yyy
#!/usr/local/bin/perl

use diagnostics;

system ("\\\cp ./xxx1 ./yyy1") == 0 || die ("Can not copy xxx1 to yyy1\n");
pds:/db0_d/_alex/hup/pmc> yyy
sh: ^P: not found
Uncaught exception from user code:
        Can not copy xxx1 to yyy1

-- 
All man's miseries derive from not being able to sit quietly in a room alone. (Pascal)



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

Date: Wed, 18 Dec 2002 14:43:03 GMT
From: helgi@decode.is (Helgi Briem)
Subject: Re: Why do beginners read files into an array?
Message-Id: <3e0088a4.1189892335@news.cis.dfn.de>

On Wed, 18 Dec 2002 08:34:49 -0500, "Jim Melanson"
<jim@jimmelanson.ca> wrote:

>When I was a "newbie" i had too many experiences of a file 
>being wiped out becuase there was too much reading and 
>writing going on at the same time.

You evidently didn't learn not to top-post and not to use
a 40 line signature.  Both are horribly rude and severely
reduce your chances of useful answers to your questions.

-- 
Regards, Helgi Briem
helgi AT decode DOT is

                           A: Top posting
                           Q: What is the most irritating thing on Usenet?
                                           - "Gordon" on apihna


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

Date: Wed, 18 Dec 2002 08:42:30 -0600
From: "William Alexander Segraves" <wsegrave@mindspring.com>
Subject: Re: Why do beginners read files into an array?
Message-Id: <atq1kc$vg0$1@slb6.atl.mindspring.net>

"Jim Melanson" <jim@jimmelanson.ca> wrote in message
news:DQ_L9.11718$i%.2235983@localhost...
> The "newbies" are learning from the experienced web application
developers.

Experienced web application developers might not be the best source of
wisdom about Perl.

> Dump the file into an array and close the dang file as quickly as you can.
> When you are on a busy site with thousands of hits per hour, you want that
> file open as little as possible. Dumping the contents to an array will
allow
> you to extract the data and manipulate the content.
>

OTOH, this strategy may not be scalable, e.g., a file larger than the
maximum size that can be processed in (what used to be known as core) memory
can lead to undesirable consequences.

> When I was a "newbie" i had too many experiences of a file being wiped out
> becuase there was too much reading and writing going on at the same time.

Dang! They must have let "newbies" loose on the file system design.

BTW, you've pronounced you "was" a "newbie"; but you still top post to this
newsgroup? ;-)

Seriously, I agree with your technical points. There are many reasons to
read a file into an array, not the least of which is convenience.

For an interesting exercise, readers might try converting the one-up mailing
label example in Chapter 11 of _LP2_ (Llama Book) to produce three-up
mailing labels. IMO, reading the address file into an array greatly
simplifies the solution to the problem of writing a partial (last) row of
labels to the picture lines of a three-label format.

Cheers.

Bill Segraves




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

Date: 18 Dec 2002 10:03:00 -0500
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: Why do beginners read files into an array?
Message-Id: <m3bs3jpdy3.fsf@mumonkan.sunstarsys.com>

"Jim Melanson" <jim@jimmelanson.ca> writes:

> The "newbies" are learning from the experienced web application
> developers. Dump the file into an array and close the dang file as
> quickly as you can. When you are on a busy site with thousands of hits
> per hour, you want that file open as little as possible. 

Slurping the file into an array is hardly the fastest way to do it.
If you need concurrent read-write access to a file, and flocking 
isn't feasible, why aren't you using a simple database application 
like BerkeleyDB?

-- 
Joe Schaefer         "I never think of the future. It comes soon enough."
                                               --Albert Einstein



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

Date: Wed, 18 Dec 2002 15:20:18 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Why do beginners read files into an array?
Message-Id: <Sm0M9.44439$4W1.36489@nwrddc02.gnilink.net>

[Do you think you could shorten your signature to less than those 40+ lines,
maybe to 4]
[Please do not top post]
[Please do not full quote]
Jim Melanson wrote:
> "Tintin" <me@privacy.net> wrote in message
> news:atpnn4$1cv63$2@ID-172104.news.dfncis.de...
>> Can anyone come up with a plausible reason why so many beginners
>> write code like:
[read whole file into array, then process array line by line]
>> Where does this logic come from?

> The "newbies" are learning from the experienced web application
> developers.

But what does Perl have to do with web application developers?

> When I was a "newbie" i had too many experiences of a file being
> wiped out becuase there was too much reading and writing going on at
> the same time.

Keeping a file open for a shorter period of time may reduce this risk but it
doesn't solve the problem at all. If you need exclusive access to a file
then your only option is proper file locking (or deamons, or ....). But by
just reducing the time a file is being accessed you are still a little bit
pregnant.

jue




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

Date: 18 Dec 2002 16:10:51 GMT
From: ctcgag@hotmail.com
Subject: Re: Why do beginners read files into an array?
Message-Id: <20021218111051.776$Ez@newsreader.com>

"Tintin" <me@privacy.net> wrote:
> Can anyone come up with a plausible reason why so many beginners write
> code like:

I'm not a beginner anymore, and I still write code like that.

>
> open FILE, "data.txt";
> @var=<FILE>;
> close FILE;

After this point, if data.txt become unavailable because, say, the NFS
mounted drive it is a symlinked into goes MIA, this program is fine.


> foreach $line (@var) {
>    print $line;
> }


If all I wanted to do was print each line of a file, I wouldn't use Perl
to do it in the first place.  So the chances are good that code like this
is a prelude to something else.  Say I now want to print the lines
backward.  All I have to do is add a strategically placed 'reverse'
statement.  If I originally wrote this as a while loop, I would have to
rewrite it as a slurp with a for loop, and then add a reverse statement.
That's definately more work.

> Where does this logic come from?

Arrays are easier to pass around.  Arrays are easier to 'unread' from.
Arrays are easier look up historical lines from.  Arrays are easier to
copy. Arrays are just easier to work with in general.

Whenever I need to use a small line-based file and can loop over
it one line at a time, I ask myself two questions:

1) How likely is it that the file will become unslurpably large in the
future? 2) How likely is it that at some point in the future I will have to
change the logic so I can't simply loop over one line at a time?

I've rarely answered 1 with an 'unlikely' and then been burned by it.
I've frequently answered 2 with an 'unlikely' and then been burned by it.


Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service              New Rate! $9.95/Month 50GB


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

Date: Wed, 18 Dec 2002 10:07:17 -0800
From: Jeff Zucker <jeff@vpservices.com>
Subject: Re: Why do beginners read files into an array?
Message-Id: <3E00B955.3070609@vpservices.com>

Tintin wrote:

> Can anyone come up with a plausible reason why so many beginners write code
> like:
> 
> open FILE, "data.txt";
> @var=<FILE>;


I think it's because the diamond operator is so magical that beginners 
don't learn all of its capabilities.  They know the diamond can be used 
to dump into an array and they know how to cycle through an array so 
that's what they do.  They haven't figured out that they can cycle 
through the diamond itself without first turning it into something else. 
    Also, they probably don't understand the difference between a file 
and the data contained in the file so for them there isn't any 
difference between cycling through an array of the data and cycling 
through the lines of the file.

-- 
Jeff



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

Date: Wed, 18 Dec 2002 12:44:37 -0600
From: brian d foy <comdog@panix.com>
Subject: Re: Why do beginners read files into an array?
Message-Id: <181220021244372619%comdog@panix.com>

In article <atpnn4$1cv63$2@ID-172104.news.dfncis.de>, Tintin
<me@privacy.net> wrote:

> Can anyone come up with a plausible reason why so many beginners write code
> like:

> open FILE, "data.txt";
> @var=<FILE>;
> close FILE;

> foreach $line (@var) {
>    print $line;
> }
> 
> Where does this logic come from?

it works, and for someone learning a language, i am not surprised
that they would do something one step at a time (i get data, now i got
data, now i process data).

remember, they are beginners :)

-- 
brian d foy, comdog@panix.com


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

Date: Wed, 18 Dec 2002 17:03:58 +0000 (UTC)
From: "Nick Djurovich" <Nick_Djurovich@NOSPAM.aimtechnology.com>
Subject: Re: Why doesn't this foreach SMTP loop work?
Message-Id: <atq9pt$jbc$1@knossos.btinternet.com>

"Ian.H [dS]" <ian@WINDOZEdigiserv.net> wrote in message
news:s2ouvu0itnt62vr3noalj05msq4q2fdt5t@4ax.com...
> -----BEGIN xxx SIGNED MESSAGE-----
> Hash: SHA1
>
> In a fit of excitement on 17 Dec 2002 09:23:56 -0800,
> webmaster@naturalbalance.com (Me) managed to scribble:
>
> > I'm trying to write a foreach loop to send mail through SMTP to a
> > group of addresses contained in a text database. Based upon previous
> > postings I've seen on this topic, I've come up with the script below,
> > but it doesn't work. I'd appreciate any advice. Thanks.
> >
> > use Net::SMTP;
> > open (FILE,'emails.txt') || die "Can't find the database: $!\n";
> > @indata = <FILE>;
> >
> > $smtp = Net::SMTP->new('netserverIP');
> > $smtp->mail( 'from@mydominaname.com' );
> >
> > foreach $i (@indata)
> > {
> > chomp($i);
> > ($Email) = split(/\|/,$i);
> > $smtp->to( '$Email' );        # recipient's address
> >  }
> >
> > $smtp->data();
> > $smtp->datasend("To: Person\n");
> >    $smtp->datasend("From: from\@mydomainname.com\n");
> >         $smtp->datasend("Subject: Your mail\n");
> >         $smtp->datasend("\n");
> > $smtp->datasend("Email Message\n");
> >         $smtp->dataend();
> >
> > Thanks.
> >
> > B.
>
> [untested]
>
> foreach (@indata) {
>   chomp($_);
>   my @Email = split(/\|/, $_);
>   foreach($Email) {
>     $smtp->to($_);
>   }
> }
>
> Does that work?
>
> the split() function is creating an array (so $Email should be @Email?)
> then you need to cycle through each element of that array as the e-mail
> address.
>
> If I'm wrong.. I look forward to reading corrections too.
>
>
> Regards,
>
>   Ian

Hi,

Not quite. If the line read ;

$Email = split(/\|/,$i);

Then you'd be right, but since there are parentheses around $Email, that
indicates that it's an array, and with only one scalar in the parentheses,
this means only the first element is being assigned from the array (as a
result of the split) to $Email.

Firstly, there are single quotes around $Email in this line, when they
should be double quotes ;

$smtp->to( '$Email' );        # recipient's address

Single quotes will give you ; To: $Email
Whereas double quotes will give you ; To: a@b.com
(assuming of course $Email = 'a@b.com')

Another problem you *may* encounter is that you cannot have
multiple To: fields in an email. Alternatively, you might try creating
a single To: with a list of addresses delimited with a semi-colon
and change the code thus ;

my $toAddresses = '';

foreach $i (@indata)
{
    chomp($i);
    ($Email) = split(/\|/,$i);
    $toAddresses .= "$Email;";
}

chop $toAddress; # remove last ';'

$smtp->to($toAddress);

Regards

Nick




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

Date: Wed, 18 Dec 2002 17:11:55 GMT
From: "Ian.H [dS]" <ian@WINDOZEdigiserv.net>
Subject: Re: Why doesn't this foreach SMTP loop work?
Message-Id: <u0b10vo6amc9ojk06qhnc61k0tafcpl69v@4ax.com>
Keywords: Remove WINDOZE to reply

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

In a fit of excitement on Wed, 18 Dec 2002 17:03:58 +0000 (UTC), "Nick
Djurovich" <Nick_Djurovich@NOSPAM.aimtechnology.com> managed to
scribble:

[snip]

> > [untested]
> >
> > foreach (@indata) {
> >   chomp($_);
> >   my @Email = split(/\|/, $_);
> >   foreach($Email) {
> >     $smtp->to($_);
> >   }
> > }
> >
> > Does that work?
> >
> > the split() function is creating an array (so $Email should be
> > @Email?) then you need to cycle through each element of that array
> > as the e-mail address.
> >
> > If I'm wrong.. I look forward to reading corrections too.

[snip]

> 
> Hi,
> 
> Not quite. If the line read ;
> 
> $Email = split(/\|/,$i);
> 
> Then you'd be right, but since there are parentheses around $Email,
> that indicates that it's an array, and with only one scalar in the
> parentheses, this means only the first element is being assigned from
> the array (as a result of the split) to $Email.
> 
> Firstly, there are single quotes around $Email in this line, when
> they should be double quotes ;
> 
> $smtp->to( '$Email' );        # recipient's address
> 
> Single quotes will give you ; To: $Email
> Whereas double quotes will give you ; To: a@b.com
> (assuming of course $Email = 'a@b.com')
> 
> Another problem you *may* encounter is that you cannot have
> multiple To: fields in an email. Alternatively, you might try
> creating a single To: with a list of addresses delimited with a
> semi-colon
> and change the code thus ;
> 
> my $toAddresses = '';
> 
> foreach $i (@indata)
> {
>     chomp($i);
>     ($Email) = split(/\|/,$i);
>     $toAddresses .= "$Email;";
> }
> 
> chop $toAddress; # remove last ';'
> 
> $smtp->to($toAddress);
> 

Nick,

Thanks for the clarification.

I've not used '($array) =' personally and don't use Perl for Web
scripting much these days either (mainly used for system scripting).

Your reply has enlightened me for sure.


Regards,

  Ian

-----BEGIN xxx SIGNATURE-----
Version: PGP Personal Privacy 6.5.3

iQA/AwUBPgCsymfqtj251CDhEQLppgCeNyT3aYjzaZQbWXRTrZr2diIfPZoAn1iG
0WyLleC/CoUmOAgIraCDfwip
=s9Tp
-----END PGP SIGNATURE-----

-- 
Ian.H  [Design & Development]
digiServ Network - Web solutions
www.digiserv.net  |  irc.digiserv.net  |  forum.digiserv.net
Scripting, Web design, development & hosting.


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

Date: Wed, 18 Dec 2002 11:39:34 -0600
From: Brandon Hoppe <bhoppe@ti.com>
Subject: Why is system call exit code factor of 256?
Message-Id: <atqbsm$btc$1@tilde.itg.ti.com>


The value returned from a system() call to a perl program returns the exit 
value * 256. For example, if the program exited with exit(2), then system() 
would return 512.

Why is this?

TIA


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

Date: 18 Dec 2002 18:39:28 GMT
From: roberson@ibd.nrc.ca (Walter Roberson)
Subject: Re: Why is system call exit code factor of 256?
Message-Id: <atqfd0$1vk$1@canopus.cc.umanitoba.ca>

In article <atqbsm$btc$1@tilde.itg.ti.com>,
Brandon Hoppe  <bhoppe@ti.com> wrote:
:The value returned from a system() call to a perl program returns the exit 
:value * 256. For example, if the program exited with exit(2), then system() 
:would return 512.

perldoc -f system

  The return value is the exit status of the program
  as returned by the "wait" call.

perldoc -f wait

  Behaves like the wait(2) system call on your system:
  it waits for a child process to terminate and
  returns the pid of the deceased process, or "-1" if
  there are no child processes.  The status is
  returned in $?.


Thus to understand this behaviour you have to read up on your
system's wait(2) system call. It likely looks somewhat like this:

  If statptr is non-zero, 16 bits of information
  called status are stored in the low-order 16 bits of the location pointed
  to by statptr.  Status may be evaluated with the macros described on
  wstat(5).

followed by a number of different macros that can be used.
If you read through the description of those macros, you will see
that the value encodes not just the exit() value but also
information about the state of the process (e.g., is it still running)
and about any signal it might have executed with. The exit() status
does not happen to be encoded in the bottom bits.


Or to put all of this a different way: "Because that's the way it is!!"

--
Contents: 100% recycled post-consumer statements.


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

Date: Wed, 18 Dec 2002 12:41:27 -0600
From: brian d foy <comdog@panix.com>
Subject: Re: Why is system call exit code factor of 256?
Message-Id: <181220021241271251%comdog@panix.com>

In article <atqbsm$btc$1@tilde.itg.ti.com>, Brandon Hoppe
<bhoppe@ti.com> wrote:

> The value returned from a system() call to a perl program returns the exit 
> value * 256. For example, if the program exited with exit(2), then system() 
> would return 512.
> 
> Why is this?

see the entry in the perlfunc man page:

   perldoc -f system

-- 
brian d foy, comdog@panix.com


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

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:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

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

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

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 V10 Issue 4282
***************************************


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