[19557] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1752 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Sep 15 14:06:42 2001

Date: Sat, 15 Sep 2001 11:05:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <1000577107-v10-i1752@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Sat, 15 Sep 2001     Volume: 10 Number: 1752

Today's topics:
    Re: How can I get the absolute path of the PERL script  (Tad McClellan)
    Re: How can I get the absolute path of the PERL script  <gnarinn@hotmail.com>
        Image dimension <primo.lgz@tiscalinet.it>
    Re: Image dimension <thomas@baetzler.de>
    Re: Image dimension <bart.lateur@skynet.be>
    Re: New to Perl, please help (Mark Jason Dominus)
    Re: New to Perl, please help (Mark Jason Dominus)
    Re: New upgrade perl problem (Mark Jason Dominus)
    Re: parsing large DNA files into smaller files (Tad McClellan)
        Please help <Kalle Anka@markisspecialisten.com>
    Re: Please help <Laocoon@eudoramail.com>
    Re: Please help (Tad McClellan)
    Re: Please help (Tad McClellan)
    Re: print name of var <weiss@kung.foo.at>
    Re: print name of var (Martien Verbruggen)
        Q: When to use eval for calling object subroutines? (B Wooster)
    Re: Q: When to use eval for calling object subroutines? <comdog@panix.com>
    Re: Q: When to use eval for calling object subroutines? (Mark Jason Dominus)
    Re: sub z { my $r = shift; $r -> {a} = { b => "c"} }; z <dave@caledvwlch.SPco.AMuk>
    Re: sub z { my $r = shift; $r -> {a} = { b => "c"} }; z (Tad McClellan)
    Re: sub z { my $r = shift; $r -> {a} = { b => "c"} }; z <thomas@baetzler.de>
    Re: sub z { my $r = shift; $r -> {a} = { b => "c"} }; z (Mark Jason Dominus)
    Re: sub z { my $r = shift; $r -> {a} = { b => "c"} }; z (EED)
    Re: substr match (Tad McClellan)
    Re: substr match <gnarinn@hotmail.com>
        Those quite handy FAQ posts <digirini@xs4all.nl>
    Re: use autouse 'Data::Dumper' => qw(Dump); (Randal L. Schwartz)
    Re: Using the c preprocessor for non c purposes <pne-news-20010915@newton.digitalspace.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 15 Sep 2001 13:34:59 GMT
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: How can I get the absolute path of the PERL script that's running
Message-Id: <slrn9q6j55.moo.tadmc@tadmc26.august.net>

!!nb <!!NOSPAM_nbagadio@bigfoot.com> wrote:

>I know about $0, but how can I get the full path and filename...


   perldoc FindBin


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Sat, 15 Sep 2001 15:06:04 +0000
From: gnari <gnarinn@hotmail.com>
Subject: Re: How can I get the absolute path of the PERL script that's running
Message-Id: <1000566364.020035654772073.gnarinn@hotmail.com>

In article <Nxro7.97$4W2.206@news-srv1.fmr.com>,
!!nb <!!NOSPAM_nbagadio@bigfoot.com> wrote:
>I know about $0, but how can I get the full path and filename...
>i.e.
>c:\test\tryme.pl vs just $0=tryme.pl

use FindBin qw($Bin $Script);

gnari


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

Date: Sat, 15 Sep 2001 17:21:52 +0200
From: "Primo" <primo.lgz@tiscalinet.it>
Subject: Image dimension
Message-Id: <9nvrjp$olr$1@nreada.inwind.it>

How I do to know the dimensions of an immage (eg. width=468 height=60)
without use of modules?






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

Date: Sat, 15 Sep 2001 17:24:28 +0200
From: Thomas Bätzler <thomas@baetzler.de>
Subject: Re: Image dimension
Message-Id: <vhs6qt4i642tvu96lan086psvpbsk491sb@4ax.com>

On Sat, 15 Sep 2001, "Primo" <primo.lgz@tiscalinet.it> wrote:
>How I do to know the dimensions of an immage (eg. width=468 height=60)
>without use of modules?

Prior knowledge? Or maybe familiarity with the particular image format
so that you know where to look for the size information in the image
file? Apart from that it's gonna be difficult without the use of
modules.

HTH,
-- 
use strict;my($i,$t,@r)=(0,'5 -.@BHJPT4acd6e2hk2lmn2o4r2s3tuz',map{ord}
split//,unpack('u*','L#`T&)QD5#0`#!!`#%1D)#08`#P05!!(3``$$"``#"0L&``('.
'"`P<!`````0$`'));$t=~s/(\d)(.)/$2x$1/eg;map{$t.=substr$t,$i,1,''while
$_--;$i++}@r;print"$t\n";# Thomas@Baetzler.de - http://baetzler.de/perl


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

Date: Sat, 15 Sep 2001 17:11:58 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: Image dimension
Message-Id: <5827qtov358cek1dvum7er6c64n32rk61e@4ax.com>

Primo wrote:

>How I do to know the dimensions of an immage (eg. width=468 height=60)
>without use of modules?

By using code as in the modules. See Image::Size. Or simùply use that
module. It actually is a plain perl module, only it (unnecessarily) uses
Autosplit/Autoload. If you strip those dependencies out, you don't even
need to install it. See
<http://bumppo.net/lists/macperl/1997/03/msg00252.html> for a recipe, in
this case specifically for MacPerl, but if you have another target
platform, simply ignore the Mac specific instructions (in casu WRT the
line endings).

Just copy the resulting file tree into a directory of your own, the
subdirectory "Image" should be at the top of that directory, and with
"use lib", you can include that directory into @INC.

See perlfaq8:

  How do I keep my own module/library directory?
  How do I add the directory my program lives in to the module/library
  search path?

-- 
	Bart.


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

Date: Sat, 15 Sep 2001 16:38:13 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: New to Perl, please help
Message-Id: <3ba383f5.f04$2df@news.op.net>
Keywords: GA, congregate, diversion, scant


In article <3ba3487c@news.iprimus.com.au>, Kevin <faithlezz@hotmail.com> wrote:
>I am new to Perl, could some body please help me by explain what are the
>following code means? Thank you very much. 

> Also, what's the different
>between 'local', 'my', 'global' variable?


A global variable is accessible anywhere in the program.  Variables in
Perl are global by default.  When you declare a variable with 'my', it
is not global; it is accessible inly inside of the current block.  (A
block is a piece of code that starts with '{' and ends with '}'.)

There are no 'local' variables.  Local tells Perl to save the value of
a global variable, and to arrange that no matter how the value
changes, the original value is restored when control exits the current
block.

http://www.plover.com/~mjd/FAQs/Namespaces.html explains this in more detail.
-- 
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


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

Date: Sat, 15 Sep 2001 16:52:54 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: New to Perl, please help
Message-Id: <3ba38763.f41$132@news.op.net>
Keywords: around, mess, moral, tempo


In article <3ba3487c@news.iprimus.com.au>, Kevin <faithlezz@hotmail.com> wrote:
>Hi,
>
>I am new to Perl, could some body please help me by explain what are the
>following code means? Thank you very much. Also, what's the different
>between 'local', 'my', 'global' variable?
>
> 1:    sub test {
> 2:        my $type = shift;
> 3:        my $start = shift;
> 4:        my $end = shift || $start;        # What is this line mean?

The function is expecting two or three arguments, including the object
it is called for:

      $object->test($Start, $End);

But the 'end' argument is optional; you can leave it out:

      $object->test($Start);

If the 'end' argument is missing (or zero), the value of $start will
be used, as if you had written

      $object->test($Start, $Start);

instead.  

'shift' means 'get the next argument'.  '||' means 'or',

        my $end = shift || $start;

This says 'get the next argument, or, if there isn't one, get the
value of $start; then put the result into $end.'

> 5:        my $class = ref($type) || $type;        # Same as this line

This is similar, but for a different purpose.  It's hard to explain
the purpose unless you already understand object-oriented programming
in Perl, but I'll try.  'test' is a method.  There are two ways to
call methods in Perl: You can call them with an object:

        $object->test($Start, $End);

or with a class:

        Class->test($Start, $End);

In the first case, $type will contain the object; in the second case,
it will contain the string 'Class'.  If $type contains an object,
ref($type) will return the class of the object.  

        my $class = ref($type) || $type; 

This says: 'Get the class of the object, or if there isn't one, get
the value of '$type', and put that into $class,'.

The next line creates a new object in class $class:

> 6:        my $self = bless {}, $class;

If you do  Fishbone->test($start) it will create a new 'Fishbone'
object.   If $carrot is a Carrot object, it will create another Carrot
object.  


> 7:        my ($key, $value);        # What is this line?

This delcares new '$key' and '$value' variables that are private to
the 'test' function.  When the 'test' function returns, these
variables will be destroyed.

> 8:        local $_ = "";        #What is $_ ?

$_ is just an ordinary variable that happens to be the default target
for pattern matching in Perl.  The expression /foo/ asks if the
contents of $_ match the pattern /foo/.

>10:       eval "/$start/" if defined($start);
>11:       eval "/$end/" if !$@ && defined($end);        # What is $@ ?

'eval' checks Perl code for syntax errors.  If there are any, $@
contains the error message.  if the code is good, $@ is false.

You need to learn to read the manual that comes with Perl. Special
variables like $_ and $@ are explained in the 'perlvar' manual.  Try
the command 'perldoc perlvar' on your computer.  If that doesn't work,
visit www.perldoc.com.


-- 
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


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

Date: Sat, 15 Sep 2001 17:04:28 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: New upgrade perl problem
Message-Id: <3ba38a1b.f6f$3b0@news.op.net>
Keywords: groat, inflow, primitivism, quick


In article <c03e0fbf.0109142341.64fe23e0@posting.google.com>,
Toni <jacapaca@navegalia.com> wrote:
>I recently tried to upgrade my perl installation from 5.00x to 5.6.1.

>or link old libs paths to news, can cause version compatibility
>problems between old modules and brand news or others ugly effects. 

It would have been easier to answer this question if you had said
which version of Perl your "5.00x" is.  The answer depends on x.

>What can I do to reuse them?

Look in the manual for the 'CPAN' module.  It has a feature called
'Autobundle' that is useful for reinstalling all of your old modules.

Autobundle will be safe even if some of the old modules were not
compatible with 5.6.1, because it will automaitcally rebuild all the
modules.


-- 
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


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

Date: Sat, 15 Sep 2001 13:34:58 GMT
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: parsing large DNA files into smaller files
Message-Id: <slrn9q6ivp.moo.tadmc@tadmc26.august.net>

Thomas Bätzler <thomas@baetzler.de> wrote:
>On Sat, 15 Sep 2001, Benjamin Goldberg <goldbb2@earthlink.net> wrote:
>>Jeff Gruen wrote:

>>> I am looking for a script that will help us to parse a large DNA
>>> sequence file into smaller text files. 
>
>>#!/usr/bin/perl -w
>>use strict;
>>@ARGV ||= ("dna.seq");
>
>This works only for scalars - with your Perl flags, one should see
>"Can't modify array dereference in logical or assignment (||=)"
>when trying to run it. Please don't post untested code! 


Even if "unrolled" to plain assignment, it won't do The Right Thing.

   @ARGV = @ARGV || ('dna.seq');

$ARGV[0] eq '1' if called with one argument.


This seems more clear anyway:

   $ARGV[0] = 'dna.seq' unless @ARGV;


>>$/ = \10000;
>
>What is that supposed to do? 


--------------------------------
Setting C<$/> to a reference to an integer, scalar containing an integer, or
scalar that's convertible to an integer will attempt to read records
instead of lines, with the maximum record size being the referenced
integer. 
--------------------------------

>Please see "perldoc perlvar" for an
>explanation of the input record separator.


Errr, right.  :-)


>>print "Processing...\n";
>>while( <> ) {
>
>Coud've fooled me - do like you're using a filename above, then revert
>to reading from STDIN :-)


The diamond operator operates on whatever filenames it finds in @ARGV,
or STDIN if @ARGV is empty.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Sat, 15 Sep 2001 14:56:12 GMT
From: "Kalle Anka" <Kalle Anka@markisspecialisten.com>
Subject: Please help
Message-Id: <g_Jo7.55828$e5.2877452@newsb.telia.net>

How do I print OUTFILE $line without the string from $email

The outfile looks like this:
someone@somewere.com
someone2@somewere.com
someone3@somewere.com

And the remove string is for example someone2@somewere.com




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

Date: Sat, 15 Sep 2001 17:04:42 +0200
From: Laocoon <Laocoon@eudoramail.com>
Subject: Re: Please help
Message-Id: <Xns911DAF3069759Laocooneudoramail@62.153.159.134>

> How do I print OUTFILE $line without the string from $email

? Do u mean on-the-fly? if not then u could do some like this
(i suppose there is a newline after each email)

$line =~ s/$email\n//;

perldoc perlre


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

Date: Sat, 15 Sep 2001 15:17:44 GMT
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Please help
Message-Id: <slrn9q6ph4.n2k.tadmc@tadmc26.august.net>

Kalle Anka <KalleAnka@markisspecialisten.com> wrote:

>How do I print OUTFILE $line without the string from $email


Huh?

What is "$email"?

If you are referring to some other post you made, then your
followup should have a References header.

Please don't break stuff like that.

Do not start a new thread when it is not a new thread, just
followup within the thread that is already there.


>The outfile looks like this:
>someone@somewere.com
>someone2@somewere.com
>someone3@somewere.com
>
>And the remove string is for example someone2@somewere.com


   perl -nle 'print unless $_ eq q(someone2@somewere.com)' <infile >outfile


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Sat, 15 Sep 2001 15:17:44 GMT
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Please help
Message-Id: <slrn9q6pa6.n2k.tadmc@tadmc26.august.net>

Kalle Anka <KalleAnka@markisspecialisten.com> wrote:


Please put the subject of your article in the Subject of your article.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Sat, 15 Sep 2001 15:36:23 +0200
From: "Stefan Weiss" <weiss@kung.foo.at>
Subject: Re: print name of var
Message-Id: <3ba35802$1@e-post.inode.at>

"Bart Van der Donck" <bart@nijlen.com> wrote:

> Is it possible to have a screen output like
>
> Variable b is set to 'hey'.
> Variable c is set to 'hello'.
> Variable lastvar is set to 'bye'.
> ...
>
> I tried arrays, hashes, but it seems very hard to print a variable's name to
> screen.

Usually, if a variable's name is important, you'd be better of using
a hash instead of scalars. In your case it looks like you're just
looking for a way to debug your script. Try this:

$b = "hey";
$c = "hello";
$lastvar = "bye";

debug(qw($b));
debug(qw($c $lastvar));

sub debug {
    print "Variable $_ is set to '".eval($_)."'.\n" for (@_);
}


cheers,
stefan







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

Date: Sat, 15 Sep 2001 23:29:30 +1000
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: print name of var
Message-Id: <slrn9q6ltq.bfe.mgjv@martien.heliotrope.home>

On Sat, 15 Sep 2001 12:47:57 GMT,
	Bart Van der Donck <bart@nijlen.com> wrote:
> Hi,
> 
> $b="hey";
> $c="hello";
> $lastvar="bye";
> ...
> 
> Is it possible to have a screen output like
> 
> Variable b is set to 'hey'.
> Variable c is set to 'hello'.
> Variable lastvar is set to 'bye'.

print "Scalar \$b is set to '$b'.\n";
print "Scalar \$c is set to '$c'.\n";
print "Scalar \$lastvar is set to '$lastvar'.\n";

Did you mean that you wanted to get a list of all the currently
'defined' variables in the current package, and list them? That is
possible, but you do not want to go there. Honestly.

Why are you interested anyway?

Besides that, anything like this would only ever work on package
variables, and you'd miss out on lexically scoped variables, which make
up the largest proportion of variables in anything I write.

Besides that, you'd need to filter out all of those predefined variables
that possibly already exist in your package in the first place (and some
of them are very messy). And you'd need to deal with the fact that Perl
has scalars, arrays, hashes, and a few other things all stashed away in
the same name slot.

> I tried arrays, hashes, but it seems very hard to print a variable's
> name to screen.

You probably do want hashes, though, if you want a group of scalars
that you can list:

$hash{b} = "hey";
$hash{c} = "hello";
$hash{lastvar} = "bye";

while (my ($key, $val) = each %hash)
{
    print "Variable $key is set to '$val'.\n";
}

Martien

PS. Do not look beyond this line.



Also see Perl FAQ 7, "How do I clear a package?"

my $stash = *{"main::"}{HASH};
foreach my $name (keys %$stash)
{
    my $val = ${"main::$name"};
    print "\$$name = '$val'\n";
}


Or, if you're interested in the current package, which isn't main::

my $stash = *{__PACKAGE__ . "::"}{HASH};
foreach my $name (keys %$stash)
{
    my $val = ${__PACKAGE__ . "::$name"};
    print "\$$name = '$val'\n";
}

-- 
Martien Verbruggen              | 
Interactive Media Division      | Useful Statistic: 75% of the people
Commercial Dynamics Pty. Ltd.   | make up 3/4 of the population.
NSW, Australia                  | 


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

Date: 15 Sep 2001 09:00:55 -0700
From: bwooster47@hotmail.com (B Wooster)
Subject: Q: When to use eval for calling object subroutines?
Message-Id: <75cc51dd.0109150800.5cd054e0@posting.google.com>

I am totally confused about some Perl OO coding - last night,
I was working with some Perl code, and I was unable
to call a member function without using eval - today, the same
code works! I am sure I did not make any other changes, am
curious why things are working without eval today!

Any thoughts on this?
  
 ....
foreach $group (@groups) {
    eval { print  $group->name; }; # this was needed last night!
    print $group->{GROUPNAME}; # prints same data as above, no eval
    print $group->name ; # works today, didn't work yesterday!
 ....

Code snippet from the aubng package.

~
~


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

Date: Sat, 15 Sep 2001 11:23:14 -0500
From: brian d foy <comdog@panix.com>
Subject: Re: Q: When to use eval for calling object subroutines?
Message-Id: <comdog-3E3F1F.11231415092001@news.panix.com>

In article <75cc51dd.0109150800.5cd054e0@posting.google.com>, 
bwooster47@hotmail.com (B Wooster) wrote:

> I am totally confused about some Perl OO coding - last night,
> I was working with some Perl code, and I was unable
> to call a member function without using eval - today, the same
> code works! I am sure I did not make any other changes, am
> curious why things are working without eval today!

sometimes that's just the way the programming gremlins
work -- you are completely convinced you are doing it
correctly even though it isn't working so you go off on
all sorts of wierd side trips only to come back the next
day to find the your starting point works. :)

-- 
brian d foy <comdog@panix.com> - Perl services for hire
CGI Meta FAQ - http://www.perl.org/CGI_MetaFAQ.html
Troubleshooting CGI scripts - http://www.perl.org/troubleshooting_CGI.html



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

Date: Sat, 15 Sep 2001 17:01:00 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: Q: When to use eval for calling object subroutines?
Message-Id: <3ba3894b.f64$124@news.op.net>

In article <75cc51dd.0109150800.5cd054e0@posting.google.com>,
B Wooster <bwooster47@hotmail.com> wrote:
>I am totally confused about some Perl OO coding - last night,
>I was working with some Perl code, and I was unable
>to call a member function without using eval

What do you mean, 'unable'?  Did you get an error message?  If so,
what was the message?  Or were you simply overcome with ennui?

>Any thoughts on this?

My thought is that without some explanation of what reall *happened*
nobody is going to have anything much useful to say.

But here's some advise which is more useful than it sounds:

>    print $group->name ; # works today, didn't work yesterday!

Never, never, never say "didn't work".  

If you ever find yourself saying that something "didn't work", just
stop and remind yourself that it would be a good time to describe what
actually happened when you tried it.

Following just that one little rule will move you out of the ranks of
the beginners.


-- 
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


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

Date: Sat, 15 Sep 2001 14:14:30 +0100
From: Dave Arnold <dave@caledvwlch.SPco.AMuk>
Subject: Re: sub z { my $r = shift; $r -> {a} = { b => "c"} }; z ($x); print $x ->  {a} -> {b}
Message-Id: <5a4f66ba4a.dave@caledvwlch.co.uk>

In message <3BA34EE8.9085179F@eed.ericsson.se>
          "Alexander Farber (EED)" <eedalf@eed.ericsson.se> wrote:

> Hi,
> 
> why doesn't this print "c":
> 
>    maas34:eedalf {101} perl -e 'sub z { my $r = shift; $r -> {a} = { b => "c"} }; z ($x); print $x -> {a} -> {b}'

In this instance z is being called with @_ set to (undef).
Therefore $r is assigned and undefined value.
You use an undefined value as a reference to a non existant hash key which
is assigned a reference to a new anonymous hash. You therefore haven't
done anything to $x.

> but this does:
> 
>    maas34:eedalf {102} perl -e 'sub z { my $r = shift; $r -> {a} = { b => "c"} }; $x = {}; z ($x); print $x -> {a} -> {b}'
> 
In this case $r is assigned the reference of the anonymous hash assigned to
$x. You use a non-existant hash key which is assigned a reference to a new
anonymous array. Perl creates the non-existant hash key for you. The first
anonymous hash therefore has a new key "a" and since $x is referencing
that hash you can use $x to access the new key.

This sort of thing is better achieved by returning the hash you create
in the subroutine:

sub z { my $r = { a => { b => "c" } }; return $r }
$x = z();

Dave.
-- 
No, the fact that it's an infinite loop doesn't mean the program doesn't
work; it just entered a state with which I was previously unfamiliar.
     Calum - Acorna, A.McCaffrey & M.Ball
                        Remove SPAM to reply


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

Date: Sat, 15 Sep 2001 15:06:43 GMT
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: sub z { my $r = shift; $r -> {a} = { b => "c"} }; z ($x); print $x ->  {a} -> {b}
Message-Id: <slrn9q6nn6.mvq.tadmc@tadmc26.august.net>

Dave Arnold <dave@caledvwlch.SPco.AMuk> wrote:
>In message <3BA34EE8.9085179F@eed.ericsson.se>
>          "Alexander Farber (EED)" <eedalf@eed.ericsson.se> wrote:
>
>> Hi,
>> 
>> why doesn't this print "c":
>> 
>>    maas34:eedalf {101} perl -e 'sub z { my $r = shift; $r -> {a} = { b => "c"} }; z ($x); print $x -> {a} -> {b}'
>
>In this instance z is being called with @_ set to (undef).
>Therefore $r is assigned and undefined value.
>You use an undefined value as a reference to a non existant hash key which
>is assigned a reference to a new anonymous hash. You therefore haven't
                                                                ^^^^^^^
>done anything to $x.
 ^^^^^^^^^^^^^^^^^^^


You haven't done anything to $x inside of z(), but the one-liner
certainly does do something to $x.

Try adding a "print $x" at the end.

Autovivification happens when you attempt to deref the undef value:

   http://tlc.perlarchive.com/articles/perl/ug0002.shtml

(but it is the print(), not z() that triggers the autoviv)

You can let autoviv do The Right Thing:

perl -e 'sub z {$_[0]-> {a} = { b => "c" } }; z ($x); print $x -> {a} -> {b}'


>This sort of thing is better achieved by returning the hash you create
>in the subroutine:
>
>sub z { my $r = { a => { b => "c" } }; return $r }
>$x = z();


That is yet another way (better than mine) of doing it.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Sat, 15 Sep 2001 15:08:12 +0200
From: Thomas Bätzler <thomas@baetzler.de>
Subject: Re: sub z { my $r = shift; $r -> {a} = { b => "c"} }; z ($x); print $x -> {a} -> {b}
Message-Id: <o3k6qt091l71qgp1em330muv98cqjmgepg@4ax.com>

On Sat, 15 Sep 2001 14:51:52 +0200, "Alexander Farber (EED)"
<eedalf@eed.ericsson.se> wrote:

>why doesn't this print "c":
>
>   maas34:eedalf {101} perl -e 'sub z { my $r = shift; $r -> {a} = { b => "c"} }; z ($x); print $x -> {a} -> {b}'
>
>but this does:
>
>   maas34:eedalf {102} perl -e 'sub z { my $r = shift; $r -> {a} = { b => "c"} }; $x = {}; z ($x); print $x -> {a} -> {b}'

What you're trying to do is a call by reference, if I interpret you
right.

What happens in sample a) is that you pass in a scalar value to z().
You then proceed to change that value to "a reference to an anon hash,
who's element 'a' is a reference to an anon hash with the element b".
Then you discard the scalar, and the whole shebang dangling on it is
deallocated.

In b) $x is not undef but an reference to an empty anon hash. You pass
this reference in to z() and your empty referenced hash is filled with
the assignment of a to an anon hash. So when you exit, it doesn't
matter that the temporary $r is discarded, because your $x on the
outside still points to the modified hash.

The moral is of course that you should not rely on such side effects,
but instead pass in a reference if you want to change your referenced
values:

#!/usr/bin/perl -w

use strict;

sub z {
  my $r = shift;
  $$r->{a} ={ b => "c"}
}

my $x;

# call by reference lets you change $x
z(\$x);

print $x->{a}->{b};

__END__

HTH,
-- 
use strict;my($i,$t,@r)=(0,'5 -.@BHJPT4acd6e2hk2lmn2o4r2s3tuz',map{ord}
split//,unpack('u*','L#`T&)QD5#0`#!!`#%1D)#08`#P05!!(3``$$"``#"0L&``('.
'"`P<!`````0$`'));$t=~s/(\d)(.)/$2x$1/eg;map{$t.=substr$t,$i,1,''while
$_--;$i++}@r;print"$t\n";# Thomas@Baetzler.de - http://baetzler.de/perl


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

Date: Sat, 15 Sep 2001 16:31:19 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: sub z { my $r = shift; $r -> {a} = { b => "c"} }; z ($x); print $x -> {a} -> {b}
Message-Id: <3ba38256.ece$d4@news.op.net>

In article <3BA34EE8.9085179F@eed.ericsson.se>,
Alexander Farber (EED) <eedalf@eed.ericsson.se> wrote:
>Hi,
>
>why doesn't this print "c":
>
>   maas34:eedalf {101} perl -e 'sub z { my $r = shift; $r -> {a} = { b => "c"} }; z ($x); print $x -> {a} -> {b}'
>

Other folks have explained this, but I don't think anyone has pointed
out that you can get exactly what you want this way:

        sub z { $_[0] = {} unless defined $_[0];
                $_[0] -> {a} = { b => "c"} 
              };

Now both examples work.   If you do:

 z ($x); print $x -> {a} -> {b}'

you get 'c', even when $x is undef.

Hope this helps.
-- 
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


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

Date: Sat, 15 Sep 2001 15:27:10 +0200
From: "Alexander Farber (EED)" <eedalf@eed.ericsson.se>
Subject: Re: sub z { my $r = shift; $r -> {a} = { b => "c"} }; z ($x); print $x  -> {a} -> {b}
Message-Id: <3BA3572E.64C7FC33@eed.ericsson.se>

"Thomas Bätzler" wrote:
> The moral is of course that you should not rely on such side effects,
> but instead pass in a reference if you want to change your referenced
> values:

Right, thanks!


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

Date: Sat, 15 Sep 2001 13:34:56 GMT
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: substr match
Message-Id: <slrn9q6hha.moo.tadmc@tadmc26.august.net>

Kalle Anka <KalleAnka@markisspecialisten.com> wrote:
>This will not remove when its a perfect match. Did I forget something?
>
># "*" IS NOT ALLOWED


   # "?" and "*" are not allowed


> if ($email =~ tr/*?//d) {                    # = form from maillist.htm
                    ^
                    ^ you are stripping question marks too


># REMOVE IF PERFECT MATCH - This one doesnt work. Nothing seems to be
>perfect match.


There are 2 parts to a pattern match. The pattern, and the string
to be matched against.

To troubleshoot a pattern match, we need both. We have only one...


>$line;


What is that for?

Do you have warnings enabled? It should be saying something
about that useless statement.


>  while (<INFILE>) {
>    chomp; # drops trailing newlines; can omit if undesirable
>    $line = $_, last if /^\Q$email/i && ! /^$/; # substr match!
>  }

------------------
#!/usr/bin/perl -w
use strict;

my $email = 'foo';

$_ = 'foo stuff';

print "matched\n" if /^\Q$email/i && ! /^$/;
------------------

Prints "matched". Looks like the pattern part is OK, must
be the undisclosed "other part" that is the problem...


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Sat, 15 Sep 2001 15:14:05 +0000
From: gnari <gnarinn@hotmail.com>
Subject: Re: substr match
Message-Id: <1000566845.278672118205577.gnarinn@hotmail.com>

In article <Y%Fo7.55787$e5.2873739@newsb.telia.net>,
Kalle Anka <Kalle Anka@markisspecialisten.com> wrote:
>This will not remove when its a perfect match. Did I forget something?

(snipped badly indented code that did not compile)

what you forgot is to create a clear example script
that exhibits your problem, but still does compile
tell us what happens, and how that differs from what you expected to happen.

then maybe more people would actually bother looking at your code.

gnari



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

Date: Sat, 15 Sep 2001 19:36:56 +0200
From: Rinus Luijmes <digirini@xs4all.nl>
Subject: Those quite handy FAQ posts
Message-Id: <hcl4qtkkcqcf6dneqtgm4vamkk3j9s3hjf@4ax.com>

Hello,
I've been lurking in this group for a while since I'm a newbie and I
already learned a lot by collection all those automatic FAQ-posts in
this group. A great invention that could be handy for some other
newsgroups too.

Greetings,
-- 
Rinus Luijmes
www.xs4all.nl/~digirini N 51°57.032' E 006°24.689'


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

Date: 15 Sep 2001 09:18:21 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: use autouse 'Data::Dumper' => qw(Dump);
Message-Id: <m1vgikqvg2.fsf@halfdome.holdit.com>

>>>>> "Martien" == Martien Verbruggen <mgjv@tradingpost.com.au> writes:

Martien> use autouse 'Data::Dumper' => qw(Dump);
Martien> my $var = { a => 2, b => [1, 2, 3] };
Martien> print Data::Dumper->Dump([$var], ['$var']);

Martien> I can't think of an easy way around this.

Trivial.  No need to use autouse.  Since it's a method, and not
an exported function, a locally defined function of the same name
is not a collision:

    sub Dump {
      require Data::Dumper;
      Data::Dumper->Dump(@_);
    }


    ...

    print Dump( ... );

I do this all the time.  "Lazy require".

print "Just another Perl hacker,"

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


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

Date: Sat, 15 Sep 2001 16:53:25 +0200
From: Philip Newton <pne-news-20010915@newton.digitalspace.net>
Subject: Re: Using the c preprocessor for non c purposes
Message-Id: <ndm6qt4iqh5m385caa0t6bkvj105haptfo@4ax.com>

On Fri, 14 Sep 2001 10:37:25 +0200, "B. Caligari"
<bcaligari@fireforged.com> wrote:

> gcc's -E let's me just run files through the C preprocessor.  How can I,
> if possible, retrieve the output of the preprocessed file?

Open a pipe to the preprocessor and read in the output from there? (e.g.
with popen())

> ----Perl related----
> I know that perl can run source code through the C preprocessor before
> interpreting.  (-P option).  However, when running ActivePerl on Win32 it
> complained that it could not find 'sed'!!!!  After installing a win32 'sed'
> it complained that there is no 'cl'.

'cl' is the name of Microsoft's compiler and linker driver, e.g. for MS
Visual C++. Windows doesn't come with a C preprocessor, or a compiler
for that matter, so I presume your Perl expects the C preprocessor with
which it was built to be present on your system.

Have you got Microsoft Visual C++ installed? If so, did you run
vcvars32.bat before trying -P? If not, which C preprocessor did you
expect Perl to use?

Cheers,
Philip
-- 
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.


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

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


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