[7317] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 942 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Aug 28 19:18:51 1997

Date: Thu, 28 Aug 97 16:00:23 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 28 Aug 1997     Volume: 8 Number: 942

Today's topics:
     Re: 2-dim arrays help please! <tom@mitra.phys.uit.no>
     Re: 2-dim arrays help please! <rootbeer@teleport.com>
     Re: 2-dim arrays help please! <tw36027@glaxowellcome.com>
     Re: Address Book in Perl? <rootbeer@teleport.com>
     Re: Bizarre result with regex in -i -pe (Jonathan Feinberg)
     Re: CGI.pm, go to another script and delaying access ti <tom@mitra.phys.uit.no>
     Re: checing for clean compile <rootbeer@teleport.com>
     Re: Chomp and chop <tom@mitra.phys.uit.no>
     Re: Compiling Perl <rra@stanford.edu>
     Re: Compiling Perl <rootbeer@teleport.com>
     Re: dbm <trandell@clark.net>
     Re: Docu ment contains no data (was :www.perl.com)? <rra@stanford.edu>
     Re: Filtering CR & CRLF from text input <tom@mitra.phys.uit.no>
     Re: Getting next byte of scalar <tom@mitra.phys.uit.no>
     Re: Getting next byte of scalar <rootbeer@teleport.com>
     Re: Integers, God, and e (was Re: Bug in perl? Bug in m (Jonathan Feinberg)
     Re: module problems <rra@stanford.edu>
     Re: my scoping, 5.002 vs 5.004 (Greg Bacon)
     Re: my scoping, 5.002 vs 5.004 <rra@stanford.edu>
     Re: my scoping, 5.002 vs 5.004 <rootbeer@teleport.com>
     Re: Perl FileHandles <rra@stanford.edu>
     Re: Perl for Win32 - Capturing output from backticks (Greg Ward)
     Re: PERL/HTML Question <trandell@clark.net>
     Re: returning from a subflow from a subflow (Jot Powers)
     Re: returning from a subflow from a subflow <rootbeer@teleport.com>
     Re: returning from a subflow from a subflow <rra@stanford.edu>
     Re: SSH and Perl <ajc@bing.wattle.id.au>
     Re: type char <cmargoli@world.northgrum.com>
     Re: Use of Backslash in PERL on NT (Michael Geary)
     Re: Wrong status from system() in child process ? <rootbeer@teleport.com>
     Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)

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

Date: 28 Aug 1997 23:07:48 +0200
From: Tom Grydeland <tom@mitra.phys.uit.no>
Subject: Re: 2-dim arrays help please!
Message-Id: <nqorabevwwr.fsf@mitra.phys.uit.no>

robert burditt <rburditt@no.spam.please.fedex.com> writes:

> you can use arrays of references [for 2-dim arrays]
> unfortunatly, i haven't been able to find an example of what i'm
> trying to do.

Your understanding is basically correct, and the perldsc manpage
(dsc == data structures cookbook) has several examples of initializing,
populating and accessing two levels of data structures, and hints on
the next levels.

> i'd like to eventually have a 2 dim array with all of
> my calcs so that i can print them out something like this:
> print "$results[$row][$index]";  etc, etc...

If you've done the first part right, that print should work fine.

> can anyone out there help me out in the proper way/syntax to
> accomplish this type of thing?

Yup.  your included documentation should help.

> rburditt@no.spam.please.fedex.com  --remove no.spam.etc.

Oh, and ch. 4 of _Programming Perl_, 2/e is called "References and
nested data structures".  Perhaps you'd care to take a look there if
you prefer paper over bright dots?

-- 
//Tom Grydeland <Tom.Grydeland@phys.uit.no>


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

Date: Thu, 28 Aug 1997 14:48:36 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: robert burditt <rburditt@no.spam.please.fedex.com>
Subject: Re: 2-dim arrays help please!
Message-Id: <Pine.GSO.3.96.970828144341.18691G-100000@julie.teleport.com>

On Thu, 28 Aug 1997, robert burditt wrote:

> $var1[$index] = a random calc
> $var2[$index] = a random calc
> $var3[$index] = a random calc

If you really want 2-d arrays, you probably want one of these kinds of
things, but from your description, I'm not sure which.

    $var[3][$index] = a random calc

    $var[$index][3] = a random calc

> my feeling is that i could keep all of the variables and
> do an explicit assignment such as $results[$row] = \$var1
> or something like that, but i want to be able to eliminate
> all of those variables and the extra steps to make the assignment.

Do you mean something like this?

    $results[$row] = [ $var1, $var2, ... ];

> can anyone out there help me out in the proper way/syntax to
> accomplish this type of thing?

If what I've written above isn't what you want, and you can't find what
you want in the manpages and FAQs, could you try asking again? Thanks!

-- 
Tom Phoenix           http://www.teleport.com/~rootbeer/
rootbeer@teleport.com  PGP   Skribu al mi per Esperanto!
Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/



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

Date: Thu, 28 Aug 1997 17:58:41 -0400
From: Thad Welch <tw36027@glaxowellcome.com>
To: robert burditt <rburditt@no.spam.please.fedex.com>
Subject: Re: 2-dim arrays help please!
Message-Id: <3405F491.749AFBA3@glaxowellcome.com>

Here's an example:

#!/pub/bioinfo/perl/bin/perl
use strict;

# load 2d array, 3 rows x 2 cols
my @data = ( 1, 2, 3, 4, 5, 6 );
my $array2d = [];
my $num;
my $i = 0;

foreach $num ( @data ) {
    if ( ($i % 2) == 0 ) {
        push( @$array2d, [] );
    }

    push( @{$array2d->[@$array2d-1]}, $num );
    $i++;
}

# display by foreach (I think this is faster than the for loop below)
my $rowRef;
my $row = 0;
my $col = 0;
$row = 0;
foreach $rowRef ( @$array2d ) {
    my $value;
    $col = 0;
    foreach $value ( @$rowRef ) {
        print "\n row: $row col: $col value: $value";
        $col++;
     }
     $row++;
}

print "\n\n";

#display the "C" way
for ( $row=0; $row<@$array2d; $row++) {
     for ( $col=0; $col<@{$array2d->[$row]}; $col++ ) {
         print "\n row: $row col: $col value: $array2d->[$row]->[$col]";

     }
}
print "\n\n";


robert burditt wrote:

> I have spent much time reading some perl books trying to figure out
> how to create, fill, and access a 2-dim array.  i am aware that
> perl doesn't directly support them, but i've also read that you
> can use arrays of references that will do basically the same thing.
> unfortunatly, i haven't been able to find an example of what i'm
> trying to do.
>
> I am reading several columns and rows out of a database.
> once i select my data, i have to perform several calculations
> on the data and then i would like to store it in a 2 dim array.
> for each row of data i select, i must perform several calcs.
>
> $var1[$index] = a random calc
> $var2[$index] = a random calc
> $var3[$index] = a random calc
>
> i have a loop that performs all of the calculations once for each
> row of data.
>
> once i have all of my calculations done(and there are lots and lots of
>
> them) i want to print them out in an html table.
>
> i'd like to eventually have a 2 dim array with all of
> my calcs so that i can print them out something like this:
> print "$results[$row][$index]";  etc, etc...
>
> my feeling is that i could keep all of the variables and
> do an explicit assignment such as $results[$row] = \$var1
> or something like that, but i want to be able to eliminate
> all of those variables and the extra steps to make the assignment.
>
> can anyone out there help me out in the proper way/syntax to
> accomplish this type of thing?
>
> thanks a lot in advance for your help!
> robert
>
> rburditt@no.spam.please.fedex.com  --remove no.spam.etc.





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

Date: Thu, 28 Aug 1997 14:37:46 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: "Juan Gonzalo Lopez R." <jglopez@pollux.javeriana.edu.co>
Subject: Re: Address Book in Perl?
Message-Id: <Pine.GSO.3.96.970828143519.18691E-100000@julie.teleport.com>

On 28 Aug 1997, Juan Gonzalo Lopez R. wrote:

> Does anyone knows how can I create a efficient address book?

Sure, lots of people know. :-)

> Using AnyDBM_File, but still is inefficient, 

That sounds like a problem in your code. DBM files should be efficient. 
(Depending, of course, on how you define efficiency.) If you post a small
example of what you find to be inefficient, somebody here may be able to
suggest what can be done to speed it up. (See if you can cut the code down
to ten or fifteen lines.) Good luck! 

-- 
Tom Phoenix           http://www.teleport.com/~rootbeer/
rootbeer@teleport.com  PGP   Skribu al mi per Esperanto!
Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/



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

Date: 28 Aug 1997 22:08:19 GMT
From: jdf@pobox.com (Jonathan Feinberg)
Subject: Re: Bizarre result with regex in -i -pe
Message-Id: <5u4ssj$es6$1@gte2.gte.net>

rootbeer@teleport.com said...

> That's because the [^\t] will match the newline, since that's not a tab. 
> (But if [^\t] didn't match it, then the $ would.) 

Hmm.  I quote perlre:

  $ Match the end of the line (or before newline at the end)

So $ shouldn't match a newline, right?  Furthermore, since $ matches
"before" the newline, then /[^\t]+$/ should match all the non-tab chars
up to, but not including, the newline at the end.  Or would that be 
/[^t]+?$/ ? Am I misinterpreting perlre?
-- 
Jonathan Feinberg    jdf@pobox.com    Sunny Manhattan, NY


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

Date: 28 Aug 1997 22:49:10 +0200
From: Tom Grydeland <tom@mitra.phys.uit.no>
Subject: Re: CGI.pm, go to another script and delaying access times...
Message-Id: <nqovi0qvxrt.fsf@mitra.phys.uit.no>

Tom Phoenix <rootbeer@teleport.com> writes:

> > Is there a code or module can I use to put a delay in my script? Like a
> > chat script I have, I need to have it so it only allows an indivildual
> > to hit update or chat, etc. once every 10 seconds or so.

> Keep a list (in a file) of every person who has triggered the script in
> the past ten seconds, and their "next time" (which is when they are
> allowed to trigger the script next). [rest snipped]

What's wrong with sleep()?  Try perldoc -f sleep.

> Hope this helps!

The same goes for this.

>    http://www.stonehenge.com/merlyn/WebTechniques/

Indeed an excellent starting point for exploring perl programming for
the WWW.

> Tom Phoenix           http://www.teleport.com/~rootbeer/

-- 
//Tom Grydeland <Tom.Grydeland@phys.uit.no>


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

Date: Thu, 28 Aug 1997 15:42:36 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: Pete Desnoyers <peted@infi.net>
Subject: Re: checing for clean compile
Message-Id: <Pine.GSO.3.96.970828153337.18691L-100000@julie.teleport.com>

On Thu, 28 Aug 1997, Pete Desnoyers wrote:

> When I run this from the command line it works fine.  I recieve the
> appropriate messges:
> 
>  /usr/local/bin/perl -wcMdiagnostics file.pl > compile.dat 2>&1
> 
> However, if I run this:
> 
> my $test = system("/usr/local/bin/perl -wcMdiagnostics file.pl >
> compile.dat 2>&1");
> 
>  via the WEB(CGI), I get this:
> 
> couldn't find diagnostic data in /usr/local/lib/perl5/pod/perldiag.pod
> /usr/local/lib/perl5/alpha-dec_osf/5.002 /usr/local/lib/perl5
> /usr/local/lib/perl5/site_perl/alpha-dec_osf
> /usr/local/lib/perl5/site_perl .
> file.pl at
> /usr/local/lib/perl5/diagnostics.pm line 225, chunk
> 507.
> BEGIN failed--compilation aborted, chunk 507.

Possibly a dumb question, but I'll ask it anyway: Are you running the same
perl binary from the command line as your webserver is? You probably
are, but I just want to be sure.

Chances are that the diagnostics data are in a file whose permissions
don't allow the user 'nobody' to read them. Or maybe one of their
enclosing directories aren't world-readable. Try this code, from the
command line, to find the files, then use 'ls -l' on the file and on the
enclosing directories to see that they're all world readable. Hope this
helps!

-- 
Tom Phoenix           http://www.teleport.com/~rootbeer/
rootbeer@teleport.com  PGP   Skribu al mi per Esperanto!
Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/

    #!/usr/local/bin/perl -w

    use Config;
    ($privlib, $archlib) = @Config{qw(privlibexp archlibexp)};
    if ($^O eq 'VMS') {
        require VMS::Filespec;
        $privlib = VMS::Filespec::unixify($privlib);
        $archlib = VMS::Filespec::unixify($archlib);
    }
    @trypod = ("$archlib/pod/perldiag.pod",
               "$privlib/pod/perldiag-$].pod",
               "$privlib/pod/perldiag.pod");
    ($PODFILE) = ((grep { -e } @trypod), $trypod[$#trypod])[0];

    if ($PODFILE) {
	print "The diagnostic data is in $PODFILE\n";
    } else {
	print "Can't find the diagnostics!\n";
    }

__END__



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

Date: 28 Aug 1997 23:01:00 +0200
From: Tom Grydeland <tom@mitra.phys.uit.no>
Subject: Re: Chomp and chop
Message-Id: <nqosovuvx83.fsf@mitra.phys.uit.no>

Nadim <nrana@aludra.usc.edu> writes:

> I am
> trying to use chomp in my script but it is not getting compiled. Why?

Perhaps your perl is too old?  What does 'perl -v' say?

> nrana@scf.usc.edu

-- 
//Tom Grydeland <Tom.Grydeland@phys.uit.no>


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

Date: 28 Aug 1997 14:21:23 -0700
From: Russ Allbery <rra@stanford.edu>
To: Arthur Merar <amerar@unsu.com>
Subject: Re: Compiling Perl
Message-Id: <m3oh6i3sx8.fsf@windlord.Stanford.EDU>

[ Posted and mailed. ]

Arthur Merar <amerar@unsu.com> writes:

> I modify my perl script.  When I try and run it, I get an error that
> says: 'Text File busy'.  The file it points to is the script.  Now, if I
> compile that, (perl -c trivia.pl), then the thing runs fine......

"Text file busy" means that your operating system is locking the file for
whatever reason and Perl can't read the script to execute it.  Are you
sure that the save from your editor had finished, that there aren't any
weird locking conditions going on, that there isn't anything wrong with
the file system that the script is located on, and so forth.

-- 
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
 00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print


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

Date: Thu, 28 Aug 1997 14:43:05 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: Arthur Merar <amerar@unsu.com>
Subject: Re: Compiling Perl
Message-Id: <Pine.GSO.3.96.970828143840.18691F-100000@julie.teleport.com>

On Tue, 26 Aug 1997, Arthur Merar wrote:

> I modify my perl script.  When I try and run it, I get an error that
> says: 'Text File busy'. 

That's not a Perl error message. Could it be coming from something else,
such as your system? Maybe because the modified file is locked by your
editor?

> The file it points to is the script.  Now, if I
> compile that, (perl -c trivia.pl), then the thing runs fine......

That compiles it, but maybe not in the sense you're thinking of. (Running
the script compiles it in the same way that -c does.) If that command does
something useful, then taking out the -c should do just as much useful
stuff, and more. :-)

Good luck!

-- 
Tom Phoenix           http://www.teleport.com/~rootbeer/
rootbeer@teleport.com  PGP   Skribu al mi per Esperanto!
Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/



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

Date: Thu, 28 Aug 1997 17:25:38 -0400
From: "Terrence J. Randell" <trandell@clark.net>
Subject: Re: dbm
Message-Id: <3405ECD1.7FB@clark.net>

May Huang wrote:
> 
> This might well be a stupid question...
> 
> I have encountered a problem when saving large chunk of data into
> dbm files.  dbm has a size limitation of 1k per entry, is there any way
> to get around this?
> 
> thanks.
> 
> --may

Try GNU's dbm.  Access with GDBM_File module.  I use that for a lot of
stuff and have no problems.  I don't know the size limit but I do know
it is plenty big.

Terrence Randell
trandell@clark.net


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

Date: 28 Aug 1997 14:37:16 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: Docu ment contains no data (was :www.perl.com)?
Message-Id: <m3g1ru3s6r.fsf@windlord.Stanford.EDU>

David Turley <dturley@rocketmail.com> writes:

> Well, www.perl.com was back, now its gone again. I found a post from a
> day or two ago mentioning this so i gues I'm not the only one. I look
> forward to the site being fixed.

> This site would probably make a good PR and reference site for promoting
> perl.

The site has *been* a fantastic PR and reference site for Perl for years.
It is currently being moved from one location to another to make it a
great deal faster, and in the process some technical difficulties have
apparently been encountered.  Please, everyone, have some patience and I'm
sure it will be back up before long.

-- 
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
 00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print


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

Date: 28 Aug 1997 22:58:27 +0200
From: Tom Grydeland <tom@mitra.phys.uit.no>
Subject: Re: Filtering CR & CRLF from text input
Message-Id: <nqou3gavxcc.fsf@mitra.phys.uit.no>

bart.mediamind@tornado.be (Bart Lateur) writes:

> The problem was how to remove CRLF's from WITHIN a text field

$field =~ tr/\r\n/ /s;

> Bart.

-- 
//Tom Grydeland <Tom.Grydeland@phys.uit.no>


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

Date: 28 Aug 1997 23:13:20 +0200
From: Tom Grydeland <tom@mitra.phys.uit.no>
Subject: Re: Getting next byte of scalar
Message-Id: <nqopvqyvwnj.fsf@mitra.phys.uit.no>

Dennis Kowalski <dennis.kowalski@daytonoh.ncr.com> writes:

> If substr has a problem with certain hex values, is there another way
> to extract a byte from a scalar when the byte may be any binary value
> of hex 00 to hex ff ?

split "C*", $buf;

used as (e.g.)

foreach $byte (split "C*", $buf) { ... }

> Thank You

You're welcome

-- 
//Tom Grydeland <Tom.Grydeland@phys.uit.no>


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

Date: Thu, 28 Aug 1997 14:58:40 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: Dennis Kowalski <dennis.kowalski@daytonoh.ncr.com>
Subject: Re: Getting next byte of scalar
Message-Id: <Pine.GSO.3.96.970828145053.18691H-100000@julie.teleport.com>

On Thu, 28 Aug 1997, Dennis Kowalski wrote:

> I am doing the following in Perl.
> 
> $byte = substr($buff,$indx,1);
> 
> $indx is incremented to get to the next byte.

That works, but if you're going to need every byte of the string, you
probably want to do that differently (for efficiency). I'd use split,
which is specially optimized for this case. Maybe like this, or maybe
another way that's better for your application.

    @bytes = split //, $buff;

    $byte = shift @bytes;		# Get a byte

> The $buff scalar has some binary data fields in it.

Multi-byte fields? Maybe you wanted unpack?

> Most of the time $byte is set to the right value but sometimes
> it gets set to 1.

It will only get set to 1 if the character you're asking for is '1'.

> One of the values that does this is a hex ac in $buff.

Does this give you anything unusual? It shouldn't...

    $buff = "\xac" x 5;				# Or whatever 
    for $indx (0..length($buff)-1) {
	$byte = substr($buff,$indx,1);		# Your code
	printf "Byte at $indx is %2x\n", ord($byte);
    }

> If substr has a problem with certain hex values, 

It doesn't. :-)  Perl is safe for binary data and fine washables. :-)

Have fun with it!

-- 
Tom Phoenix           http://www.teleport.com/~rootbeer/
rootbeer@teleport.com  PGP   Skribu al mi per Esperanto!
Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/



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

Date: 28 Aug 1997 22:16:44 GMT
From: jdf@pobox.com (Jonathan Feinberg)
Subject: Re: Integers, God, and e (was Re: Bug in perl? Bug in my code?)
Message-Id: <5u4tcc$es6$2@gte2.gte.net>

tom@mitra.phys.uit.no said...
> I'm no expert on QED, but please bear in mind that all numerical
> constants are the result of the system of units you choose to employ.
> Presumably, there exists a system of units where n is unity.

And in which everything that used to be worth some rational k is
worth k/n (where n is the old, irrational, empirically determined
value).  But not only am I not a physicist: I have NO IDEA
WHATSOEVER what I'm talking about.  I've therefore redirected
follow-ups appropriately, I think.  Back to work...
-- 
Jonathan Feinberg    jdf@pobox.com    Sunny Manhattan, NY


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

Date: 28 Aug 1997 14:42:10 -0700
From: Russ Allbery <rra@stanford.edu>
To: gt1535b@prism.gatech.edu
Subject: Re: module problems
Message-Id: <m3d8my3ryl.fsf@windlord.Stanford.EDU>

gt1535b <gt1535b@prism.gatech.edu> writes:

> Can someone please help me out with this module exporting thing?  I'm
> trying to export some subroutines into my main package so I don't have
> to qualify the name of the subroutine every time I want to use it.

Umm, unless you're doing something really strange, this is actually an
extremely simple thing to do.  Take a look at man perlmod for lots of
examples; the basic idea is:

        package Module::Name;
        use Exporter;
        @ISA    = qw(Exporter);
        @EXPORT = qw(sub1 sub2 $variable1 $variable2);

and that's all there is to it.  Then, when someone does a:

        use Module::Name;

sub1, sub2, $variable1, and $variable2 will automatically show up as
accessible.

(BTW, as a side question to other readers, what's the rationale for
putting the above use, @ISA, and @EXPORT assignments into a BEGIN {}
block?  I've never had any problems with them as is.)

-- 
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
 00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print


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

Date: 28 Aug 1997 20:56:23 GMT
From: gbacon@adtran.com (Greg Bacon)
To: colink@latticesemi.com (Colin Kuskie)
Subject: Re: my scoping, 5.002 vs 5.004
Message-Id: <5u4oln$dh3$2@info.uah.edu>

[Posted and mailed]

In article <5u4e28$fch@sarek.latticesemi.com>,
	colink@latticesemi.com (Colin Kuskie) writes:
: I wrote and released my first module last week, and now my inbox is full
: of people complaining that it produces warnings using 5.004_anything.
: Naturally, I assumed they were wrong, but after my sysadmin compiled
: 5.004_03 this morning, I have to say that something fishy is going on.

The fish lies in your code.

: Here's a quick test script:
: 
: package A::Foo;
: 
: package A::Foo::Bar;
: my $var = 12;
: 1;
: 
: package A::Foo::Baz;
: my $var = 13;
: 1;
: 
: package A::Foo;
: 1;
: 
: package main;
: my $var = 14;
: print "Hello world\n";
: 
: Running this under 5.002 produces no warnings of any kind.  However, 5.004_03
: says:
: "my" variable $var masks earlier declaration in same scope at try line 9.

This is correct behavior.  Your code is bad.  Lexicals don't live in a
package.

: Here's my quandry:
: 1)using use strict is a good thing
: 2)that says I need to use my
: 3)my causes different scoping than unmy'ed variables
: 
: I can also think of 3 or 4 different solutions to this problem, (the easiest is
: not to declare different packages in the same file).  But why doesn't package
: define a new lexical scope?

Because it's just the way it is. :-)  Only blocks and files define
scope.  (Files work because each file has implicit braces around it.)
If you want to have colliding lexicals in seperate packages in the same
file, you could always provide the braces explicity like

    {
        package Foo;

        my $a = 'fooish a';

        $bar = 'Foo::bar';
    }

    {
        package Bar;

        my $a = 'barish a';

        $foo  = 'Bar::foo';
    }

    my $a = 'outer a';

which is a more reasonable facsimile of the actual behavior of splitting
packages into seperate files. :-)  Be careful not to let closures bite
you, though.

Hope this helps,
Greg
-- 
open(G,"|gzip -dc");$_=<<EOF;s/[0-9a-f]+/print G pack("h*",$&)/eg
f1b88000b620f22320303fa2d2e21584ccbcf29c84d2258084
d2ac158c84c4ece4d22d1000118a8d5491000000
EOF


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

Date: 28 Aug 1997 14:49:21 -0700
From: Russ Allbery <rra@stanford.edu>
To: colink@latticesemi.com (Colin Kuskie)
Subject: Re: my scoping, 5.002 vs 5.004
Message-Id: <m37md63rmm.fsf@windlord.Stanford.EDU>

[ Posted and mailed. ]

Colin Kuskie <colink@latticesemi.com> writes:

> I wrote and released my first module last week, and now my inbox is full
> of people complaining that it produces warnings using 5.004_anything.
> Naturally, I assumed they were wrong, but after my sysadmin compiled
> 5.004_03 this morning, I have to say that something fishy is going on.

Yes, you're misunderstanding the difference between lexical scoping and
package scoping.  It's a very common mistake.

> package A::Foo::Bar;
> my $var = 12;
> 1;

The variable $var is *not* placed into A::Foo::Bar here, and it is *not*
the same thing as $A::Foo::Bar::var, which is an entirely different
variable.  Because you've declared it with my, it's a lexically scoped
variable that isn't entered into the package symbol table; since the scope
in which you've declared it is file level, it's available to the entire
file.

> package A::Foo::Baz;
> my $var = 13;
> 1;

This use of $var here shadows the previous one since they're both in the
same scope; you're redeclaring a lexical variable in the same scope.  This
has always been the way that my works, but just with 5.004 a warning was
added to let you know what's actually going on.

If you want to create package variables, you can't use my.  You need to
use normal variables and use "use var" to predeclare them.  If you want to
do the above with lexical scoping, do something like:

{
    package A::Foo::Baz;
    my $var = 13;
}

> I can also think of 3 or 4 different solutions to this problem, (the
> easiest is not to declare different packages in the same file).  But why
> doesn't package define a new lexical scope?

Thinking of a package as a scope is bad; it's not.  All packages are are a
single variable that Perl keeps track of, namely the name of the current
package.  All that a package statement does is change Perl's understanding
of what the current package is.  That's it.  It's not a scope in any way,
shape, or form.

-- 
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
 00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print


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

Date: Thu, 28 Aug 1997 15:02:44 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: Colin Kuskie <colink@latticesemi.com>
Subject: Re: my scoping, 5.002 vs 5.004
Message-Id: <Pine.GSO.3.96.970828145910.18691I-100000@julie.teleport.com>

On 28 Aug 1997, Colin Kuskie wrote:

> package A::Foo::Bar;
> my $var = 12;
> 1;
> 
> package A::Foo::Baz;
> my $var = 13;

That's a second my $var in the same scope. (Package directives don't
create new scopes.) So Perl is right to give you an error message.

> Running this under 5.002 produces no warnings of any kind.  

That was a bug in 5.002, fixed in 5.004.

> But why doesn't package define a new lexical scope? 

Because that's the job of a block or file or eval. :-)

    {
	package What::Ever;
	my $var = 199;		# No problem; new scope!
	 ...

Hope this helps!

-- 
Tom Phoenix           http://www.teleport.com/~rootbeer/
rootbeer@teleport.com  PGP   Skribu al mi per Esperanto!
Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/



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

Date: 28 Aug 1997 14:28:00 -0700
From: Russ Allbery <rra@stanford.edu>
To: mcgrattm@cts.com
Subject: Re: Perl FileHandles
Message-Id: <m3k9h63sm7.fsf@windlord.Stanford.EDU>

[ Posted and mailed. ]

Michael McGrattan <mcgrattm@cts.com> writes:

> I have a question regarding Perl file handles.  If I pass a filehandle
> reference to a subroutine, how can I use that reference as the handle
> for Perl format output?

[...]

> format REPORT_TOP=
> format REPORT=

[...]

>    #THIS IS WHERE I HAVE THE PROBLEM.  DO I NEED
>    #TO ASSIGN A NEW FILE HANDLE TO THE EXISTING REFERENCE?
>    #I KNOW THE BELOW DOES NOT WORK, BUT I CAN NOT FIGURE OUT
>    #HOW TO ASSIGN THE FORMAT VARIABLES TO MY EXISTING REFERENCE
> ?????  open(REPORTOUT,">$fh");  ??????

Please don't yell.  No, all you have to do is just write to the passed
file handle, after selecting it and setting its formats:

        $oldfh = select ($fh);
        $~ = 'REPORT';
        $^ = 'REPORT_TOP';
        select ($oldfh);

        write $fh;

-- 
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
 00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print


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

Date: 28 Aug 1997 21:40:58 GMT
From: greg@bic.mni.mcgill.ca (Greg Ward)
Subject: Re: Perl for Win32 - Capturing output from backticks
Message-Id: <5u4r9a$1n3@sifon.cc.mcgill.ca>

Chris Dumas (cdumas@ibm.net) wrote:
:  I am trying to calculate the freespace on a directory. The only way I
: can think of doing it is to capture the output from the DIR command and
: process the output.

: $dir_out = 'dir';

: This however fails.  Does anyone know why?????

Two possibilities: first, those aren't backticks.  They're forward
single-quotes.

Second, even if you were using the right syntax, are you sure backticks
work under Windoze?  Backticks are presumably implemented using pipes,
and I don't know if Windoze (even Win32) works like Unix in this area.
RTFM.  (For your Perl port, that is.)

        Greg
--
Greg Ward - Research Assistant                     greg@bic.mni.mcgill.ca
Brain Imaging Centre (WB201)           http://www.bic.mni.mcgill.ca/~greg
Montreal Neurological Institute           voice: (514) 398-4965 (or 1996)
Montreal, Quebec, Canada  H3A 2B4           fax: (514) 398-8948


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

Date: Thu, 28 Aug 1997 17:32:04 -0400
From: "Terrence J. Randell" <trandell@clark.net>
Subject: Re: PERL/HTML Question
Message-Id: <3405EE53.2B23@clark.net>

Rameet Kohli wrote:
> 
> How do I pass arguements from a hyperlink to a perl script?
> 
> Rameet
> 
> kohli@cs.ucdavis.edu

Just send as a query string like this:

http://www.fo.com/directory/script.cgi?variable1=name1&variable2=name2&...&variablen=namen

Then use something like this snipet to process the query string into a
hash:

$query = $ENV{'QUERY_STRING'};
@pairs = split /&/, $query;
%FORM = ();
foreach $pair (@pairs)
{
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    # Stop people from using subshells to execute commands
    $value =~ s/~!/ ~!/g;

    $FORM{$name} = $value;
}

Terrence Randell
trandell@clark.net


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

Date: 28 Aug 1997 21:48:05 GMT
From: news@bofh.com (Jot Powers)
Subject: Re: returning from a subflow from a subflow
Message-Id: <5u4rml$3qc$1@gazette.corp.medtronic.com>

In article <3405D0DF.5C29@wv.MENTORG.COM>, WX Guest account wrote:
>I have run into a problem that I can't put a finger on.  I have several
>small subroutines in an include file.  Within the file, I often call one
>subroutine from another.  When I tried to return a value from a routine
>called from another routine, a got an error.  I can't find any
>documented reason for this.  Any ideas??

Works for me.  What have you tried?

(Hint:  Always try to reduce your problem to the smallest subset
and see if it is still a problem.  I can not count the number of
times I have done this and, sure enough, the problem is with
my code.)

FWIW:

node127% cat /tmp/test.pl
#!/usr/local/bin/perl -w
use strict;
 
my ($final) = (0);
$final = &sub1();
 
print "Result was:  $final\n";
 
sub sub1 {
  my ($val1) = (1);
  $val1 = &sub2();
  return $val1;
}
 
sub sub2 {
  my ($val2) = (2);
  return $val2;
}

node127% /tmp/test.pl
Result was:  2


-- 
Jot Powers  news@bofh.com
Unix System Administrator
"Sometimes you just have to grab the bull by the tail and face the situation."


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

Date: Thu, 28 Aug 1997 15:05:51 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: WX Guest account <wxguest1@wv.MENTORG.COM>
Subject: Re: returning from a subflow from a subflow
Message-Id: <Pine.GSO.3.96.970828150358.18691J-100000@julie.teleport.com>

On Thu, 28 Aug 1997, WX Guest account wrote:

> I have several small subroutines in an include file.  Within the file, I
> often call one subroutine from another.  When I tried to return a value
> from a routine called from another routine, a got an error.  I can't
> find any documented reason for this. 

Maybe you should document your subroutines better. :-)  Seriously, if you
can cut down your problem to (say) a dozen lines which don't do what you
expect, post that here and we can take a look at it. It's hard to debug
descriptions. :-)

-- 
Tom Phoenix           http://www.teleport.com/~rootbeer/
rootbeer@teleport.com  PGP   Skribu al mi per Esperanto!
Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/



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

Date: 28 Aug 1997 14:50:30 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: returning from a subflow from a subflow
Message-Id: <m33enu3rkp.fsf@windlord.Stanford.EDU>

WX Guest account <wxguest1@wv.MENTORG.COM> writes:

> I have run into a problem that I can't put a finger on.  I have several
> small subroutines in an include file.  Within the file, I often call one
> subroutine from another.  When I tried to return a value from a routine
> called from another routine, a got an error.

What error?  We can't help you unless you let us know what the problem
is.  I call subs from other subs all the time without any problems.

Note that if you want the return value to propagate back out, you of
course have to return it from each level of subroutine until you get back
to the original caller.

-- 
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
 00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print


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

Date: 28 Aug 1997 21:54:58 GMT
From: Andrew J Cosgriff <ajc@bing.wattle.id.au>
Subject: Re: SSH and Perl
Message-Id: <ruivi0qj7mg.fsf@goaway.cc.monash.edu.au>

>>>>> "jjune" == jjune  <jjune@midway.uchicago.edu> writes:

jjune> Hi!
jjune> I am currently trying to write a perl script that will utilize
jjune> ssh and scp to

jjune> 1)  Copy files to many different servers automatically;
jjune> 2)  Execute commands on many different servers automatically;

jjune> Basically I need to be able to execute the same command on
jjune> multiple servers... for example... I will need to scp a file (a
jjune> tar file) to 30 different servers... and execute a command to
jjune> untar the file on all 30 servers.

jjune> I think I can set up the ssh with RSA to ssh in and out of all
jjune> the servers without passwords... so i'm in the process of
jjune> writing a perl script which will basically read in the server
jjune> ips and do a system call on them...

jjune> turning out to be MUCH more tricky then I had
jjune> anticipated... has anyone seen any script that does this?... Or
jjune> better yet... can anyone offer any pointers or advice on how to
jjune> go about writing something like this?

The Perl source comes with some example scripts called "gsh" and "gcp" 
and such, that use rsh - they could be modified to use ssh without too 
much trouble...

-- 
 - Andrew J Cosgriff -                                   ajc@bing.wattle.id.au
                                      ==
            I'm in direct contact with many advanced fun CONCEPTS.


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

Date: Thu, 28 Aug 1997 19:54:58 GMT
From: Charles Margolin <cmargoli@world.northgrum.com>
Subject: Re: type char
Message-Id: <3405D792.FC1@world.northgrum.com>

the count wrote:
> 
> In C you can use type char, which can be treated as a number or a letter
> depending on context.  But in perl, it will treat the number as a string
> containing those numbers, not as a letter (so if $i = 65, in character
> context it is "65" and not "A", of whatever ascii character that is)
> Is there a trivial way to force it to behave like C's char type?
> 
> In particular, I have a series of fields I am extracting from a dataset,
> each of which has almost the same name, but for one letter (e.g.
> fnma, fnmb, fnmc, etc.).  Right now I am doing something like:
> 
> for ($i = 0; $i < 4; $i++) {
>     if ($i == 0) { $a = "a"; }
>     if ($i == 1) { $a = "b"; }
>     if ($i == 2) { $a = "c"; }
>     if ($i == 3) { $a = "d"; }
>     if ($i == 4) { $a = "e"; }
> 
>     $fname = &field($id, "fnm$a");
> 
>     print "$fname\n";
> }
> 
> But am curious if there is a better way then simply assigning the letter
> based on the number, as per the above (I'm sure there is - I just can't
> come up with it off the top of my head :)

Check out the chr function (and its inverse, ord).

Also consider the "magic" autoincrement operator as described
on page 79 of the Blue Camel.

Charles Margolin  cmargoli@world.northgrum.com


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

Date: Thu, 28 Aug 1997 21:58:59 GMT
From: Mike@bitbucket.Geary.com (Michael Geary)
Subject: Re: Use of Backslash in PERL on NT
Message-Id: <3405f3e7.101507540@news.ricochet.net>

marcille@kodak.com (Phil Marcille ) wrote:

>        We are starting to use PERL as our scripting language on NT and are 
>running into problems with the use of the \ character. In PERL this denotes 
>that the next character should be taken as a special character but in NT, it's 
>the directory seperator. We want to create a series of commands(actually calls 
>to an Oracle database)  by setting them to a variable and then running them 
>with the system command.        
>ex.  $command = "plus33 sys/passwd \@test.sql > " . $ENV{'LOGS'} . 
>                       $file_seperator . "install_roles.log";
>      system($command);                 
>
>        The $file_seperator varaible was set to "\\" and LOGS = 
>d:\ORACLE_BASE\admin\ORCL\logbook.  We find that this works for the first 
>reference of the environmental variable or the $file_seprerator variable but 
>after successive references to them, the \\ is dropped and the directory 
>structure is taken as one long word.      

NT, along with Win95 and DOS, will accept either \ or / as a directory
separator. I usually avoid this problem by simply using / instead.
OTOH, many programs refuse to accept the / in their command line
parameters. So if this "plus33" program you're running doesn't like
the /, I'll have to defer to a Perl expert on how to handle it.

-Mike

To reach me by email, either use the link in my Web site at
www.geary.com, or delete "bitbucket." from my email address.
My apologies for the inconvenience provoked by our friends
the spammers.  <sigh>


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

Date: Thu, 28 Aug 1997 15:31:23 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: "Richard J. Marisa" <rjm2@cornell.edu>
Subject: Re: Wrong status from system() in child process ?
Message-Id: <Pine.GSO.3.96.970828151206.18691K-100000@julie.teleport.com>

On Thu, 28 Aug 1997, Richard J. Marisa wrote:

> I'd like to get the return status from a program
> executed via system()
> 
> However, system returns -1; $rc (as computed below) is 255.
> $! contains "No child processes"

That's odd. Are you certain that system is returning -1? (I ask only
because the code as posted doesn't print out the actual return value from
system.) That sounds as if something reaped the child process's status
before Perl could - which shouldn't happen with system. Could be a bug in
your Perl. What version are you running, and does it pass all of the tests
in the test suite, especially ./t/op/exec.t? 

> The command DOES execute properly (file is fetched via http)

Why aren't you just using LWP? :-)

>       use integer;
>       my @args = ("webget", 
>                   "-d", 
>                   "-nab=$archivedir/$jobid/$subdocnum",
>                   $url);
>       $rc = system @args;

Yep, that should be working, as far as I can see. (I wonder whether 'use
integer' is messing something up, but I can't imagine how it could.)

>       &os_errorlogger("Message: $!");
>       $rc = ($rc & 0xffff) >> 8;
>       &os_errorlogger("Job $jobid: webget returned $rc");

If system is really returning -1, I think it's a bug. If you still see
that happening with 5.004_03, run perlbug to file a bug report. Good luck!

-- 
Tom Phoenix           http://www.teleport.com/~rootbeer/
rootbeer@teleport.com  PGP   Skribu al mi per Esperanto!
Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/



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

Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 8 Mar 97)
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.misc (and this Digest), send your
article to perl-users@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.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

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 V8 Issue 942
*************************************

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