[13046] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 456 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Aug 11 01:07:19 1999

Date: Tue, 10 Aug 1999 22: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)

Perl-Users Digest           Tue, 10 Aug 1999     Volume: 9 Number: 456

Today's topics:
    Re: Anon Subs and Parameters (Martien Verbruggen)
    Re: Anon Subs and Parameters <uri@sysarch.com>
    Re: Anon Subs and Parameters (Ronald J Kimball)
    Re: BEGIN's in subs <uri@sysarch.com>
    Re: bug in 'unpack'? <hal_mounce@amdahl.com>
    Re: bug in 'unpack'? <tye@metronet.com>
        configure error!! <eskim@tysystemhouse.com>
    Re: Custer's Last Code (Sam Holden)
    Re: exists problem (Larry Rosler)
    Re: Help - Split Function Blowing My Mind Away!! (Ronald J Kimball)
    Re: Improving speed of a sub (Sam Holden)
    Re: Improving speed of a sub (brian d foy)
    Re: list length (Abigail)
    Re: list length (Abigail)
    Re: Need help using OO Perl (Martien Verbruggen)
    Re: perl script - help (Abigail)
    Re: pricing a perl job (Abigail)
    Re: pricing a perl job (David H. Adler)
    Re: reference to object method (Damian Conway)
        Script Problem <jsmith19991@my-deja.com>
    Re: Script Problem (Martien Verbruggen)
    Re: Script Problem (Bill Moseley)
        Some error I can't fix...need help please. <seongbae@students.uiuc.edu>
    Re: Strange File Behavior <berube@odyssee.net>
    Re: Strange File Behavior (Martien Verbruggen)
    Re: Strange File Behavior (Larry Rosler)
    Re: turn $6 into $6000 (Larry Rosler)
    Re: turn $6 into $6000 <uri@sysarch.com>
        Digest Administrivia (Last modified: 1 Jul 99) (Perl-Users-Digest Admin)

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

Date: Wed, 11 Aug 1999 03:13:40 GMT
From: mgjv@comdyn.com.au (Martien Verbruggen)
Subject: Re: Anon Subs and Parameters
Message-Id: <ET5s3.153$O21.8288@nsw.nnrp.telstra.net>

In article <7oqjiq$a0r$1@nnrp1.deja.com>,
	bpruett@my-deja.com writes:
> I have a problem with anon subroutines and parameters.

And with reading documentaiton.

> Can I pass a param to a code reference of an anon sub? For example.

# perldoc perlref

# perl -wl
my $cref = sub{ print @_ };
&$cref('Attempt', 1);
$cref->('Attempt', 2);
__END__
Attempt1
Attempt2

Martien
-- 
Martien Verbruggen                  | 
Interactive Media Division          | If at first you don't succeed, try
Commercial Dynamics Pty. Ltd.       | again. Then quit; there's no use being
NSW, Australia                      | a damn fool about it.


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

Date: 10 Aug 1999 23:55:50 -0400
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Anon Subs and Parameters
Message-Id: <x7pv0v9dix.fsf@home.sysarch.com>

>>>>> "MOP" == Matthew O Persico <mpersico@erols.com> writes:

you should know better than to use Jeopardy.pm!

  MOP> Try
  MOP> $theref->($para);

that doesn't solve the problem as written. but it was written badly.

  MOP> bpruett@my-deja.com wrote:
  >> 
  >> $theref = sub { $this->sub_in_this_package };
  >> 
  >> $result = &$theref($para);

that will call it and pass $para, but your anon sub doesn't do anything
with @_ so it won't see it.

the code $this->method will get passed only $this as its first arg so it
can't see what is passed to the anon sub.

so the sub should be:


$theref = sub { $this->sub_in_this_package( @_ ) };

$result = &$theref($para);

or

$result = $theref->($para);

uri

-- 
Uri Guttman  -----------------  SYStems ARCHitecture and Software Engineering
uri@sysarch.com  ---------------------------  Perl, Internet, UNIX Consulting
Have Perl, Will Travel  -----------------------------  http://www.sysarch.com
The Best Search Engine on the Net -------------  http://www.northernlight.com
"F**king Windows 98", said the general in South Park before shooting Bill.


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

Date: Wed, 11 Aug 1999 00:22:22 -0400
From: rjk@linguist.dartmouth.edu (Ronald J Kimball)
Subject: Re: Anon Subs and Parameters
Message-Id: <1dwcgva.1qezkhc61y0klN@p144.tc19a.metro.ma.tiac.com>

<bpruett@my-deja.com> wrote:

> Can I pass a param to a code reference of an anon sub? For example.
> 
> # Get a reference to the sub
> $theref = sub { $this->sub_in_this_package };

$theref = sub { $this->sub_in_this_package(@_) };

> When I do something like this the sub does run, but the parameter never
> gets to it. Have I missed something or is that just not possible?

I believe you need to pass @_ through to the method explicitly, as shown
above.

-- 
 _ / '  _      /       - aka -
( /)//)//)(//)/(   Ronald J Kimball      rjk@linguist.dartmouth.edu
    /                                http://www.tiac.net/users/chipmunk/
        "It's funny 'cause it's true ... and vice versa."


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

Date: 10 Aug 1999 23:12:17 -0400
From: Uri Guttman <uri@sysarch.com>
Subject: Re: BEGIN's in subs
Message-Id: <x7vhan9fji.fsf@home.sysarch.com>

>>>>> "AA" == Andrew Allen <ada@fc.hp.com> writes:

  AA> Although perlref briefly mentions that "In the general case, then,
  AA> named subroutines do not nest properly, although anonymous ones do", I
  AA> was wondering why the following code behaves as it does:

  AA>   sub a
  AA>   {
  AA>     my $c;
  AA>     BEGIN {$c=7;}
  AA>     print "c=$c\n";
  AA>   }
  
  AA>   a();
  AA>   a();

  AA> prints out

  AA>   c=7
  AA>   c=

  AA> I'd expect either $c to always to be 7, or always to be undef.

because the BEGIN is only executed once at compile time. if you really
want something like this put the var outside the sub in a block:

{
my $c ;
BEGIN { $c = 7 ; }
	sub a {

		blah
	}
}

uri

-- 
Uri Guttman  -----------------  SYStems ARCHitecture and Software Engineering
uri@sysarch.com  ---------------------------  Perl, Internet, UNIX Consulting
Have Perl, Will Travel  -----------------------------  http://www.sysarch.com
The Best Search Engine on the Net -------------  http://www.northernlight.com
"F**king Windows 98", said the general in South Park before shooting Bill.


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

Date: Tue, 10 Aug 1999 21:25:19 -0700
From: Hal Mounce <hal_mounce@amdahl.com>
Subject: Re: bug in 'unpack'?
Message-Id: <37B0FB2F.88B48B34@amdahl.com>

Tony Cox wrote:
> 
> I'm running the same script on the same data file to grab some data from a
> binary file on two different machines
> 

> $DWORD = 4
> $sn_loc_offset = 12353

An odd offset for a 4 byte word.  Very suspicious.
Watch those semicolons.


> open (IN, $filename) or die "Cannot open target data file: $!";
> seek (IN,$sn_loc_offset,$DWORD);
> $byte_count = read (IN, $snbuffer,$DWORD);
> close (IN);
> if ($byte_count){

A bad thing could happen for byte_count of 1, 2, or 3.

>         $snbuffer = unpack("H8", $snbuffer);
>         $sn_offset = hex($snbuffer);
> }
> 
> On one platform (5.00404) I get the correct answer for $sn_offset: 151995

That's funny.  You only have three bytes of stuff here. x'0251bb'  Of
course, I'm from Blefuscu.

> On the other (5.00503) I get $sn_offset: 1094863174 (and my program blows
> up)

But this looks so much better, x'41444946' = c'ABIF'

> Surely this can't be right? Any help or advice would be gratefully received.

It kinda depends on what was at filename + x'3041' all this time.  What
happens with xd -j 12353 -N 4 filename  (perhaps od on your platform).

Hal


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

Date: 10 Aug 1999 23:34:05 -0500
From: Tye McQueen <tye@metronet.com>
Subject: Re: bug in 'unpack'?
Message-Id: <7oquft$q47@beanix.metronet.com>

"Tony Cox" <avc@sanger.ac.uk> writes:
) The code runs:
) [snip]
) $DWORD = 4
) $sn_loc_offset = 12353
) [snip]
) 
) open (IN, $filename) or die "Cannot open target data file: $!";
) seek (IN,$sn_loc_offset,$DWORD);

The third argument to seek() should be 0, 1, or 2, not 4.
You should really check whether or not seek() succeeded
just like you did for open().

) $byte_count = read (IN, $snbuffer,$DWORD);
) close (IN);
) if ($byte_count){
)         $snbuffer = unpack("H8", $snbuffer);
)         $sn_offset = hex($snbuffer);
) }
) 
) On one platform (5.00404) I get the correct answer for $sn_offset: 151995
) On the other (5.00503) I get $sn_offset: 1094863174 (and my program blows
) up)

1094863174 is obviously C<unpack("L","1296")>.  I don't suppose
that the first four bytes of your file are "1296".
-- 
Tye McQueen    Nothing is obvious unless you are overlooking something
         http://www.metronet.com/~tye/ (scripts, links, nothing fancy)


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

Date: Wed, 11 Aug 1999 13:17:01 +0900
From: "±èÀÀ¼®" <eskim@tysystemhouse.com>
Subject: configure error!!
Message-Id: <7oqtko$qgf$1@news1.kornet.net>

hello everyone!!!
I have a problem
I try to set perl 5.004
but  sh ./Configure ..
at the end ,
follow message appear.
and stop!
typed make..
and then equal message happen
what is the problem?
so help me ..who knows
os : sunOS 5.6
Generic sun4u sparc SUNW,Ultra-1
gcc 2.8.1

ld.so.1: make: fatal: libc.so.1: version `SUNW_1.18' not found (required by
fil)
Killed








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

Date: 11 Aug 1999 03:06:41 GMT
From: sholden@pgrad.cs.usyd.edu.au (Sam Holden)
Subject: Re: Custer's Last Code
Message-Id: <slrn7r1q6q.rel.sholden@pgrad.cs.usyd.edu.au>

On Tue, 10 Aug 1999 18:09:57 -0700, Frank Krul <fkrul@acc.com> wrote:
>
>
>
>> Read up on taint checking in the perl documentation and it all should be
>> explained. Look in the 'perldoc perlsec'
>>
>> And do you really have executables in /www. Good values for PATH are
>> /bin:/usr/bin, even better is nothing at all. If you run external programs
>> then use the full path names (use some config variables to aid in porting
>> the script of course).
>
>I used absolute paths for the execs.  This is the new error:
>
>Insecure dependency in exec while running setuid at /dev/fd/3 line 123.
>Content-type: text/html
>
>I'll read the documentation, thank you for the pointer.  I sort of got thrown into
>perl with one Orielly book that does not cover this sort of thing, and a unix box
>without perl manpages.

The documentation will answer the question. Reading the documentation first
would be good.

In all likely hood you are using a value that you recieved from the outside 
world without first capturing the safe bits with a regex.

From another thread I see you program web stuff in C sometimes. Getting
something to work in perl with taint checking should give you nightmares
until you go back and change all those C programs so that they have the
some paranoid outlook that perl does.

I think I must be more paranoid then perl, since when I write web stuff
with -wT I don't see these errors. ;)


-- 
Sam

You can blame it all on the internet. I do...
	--Larry Wall


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

Date: Tue, 10 Aug 1999 20:39:36 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: exists problem
Message-Id: <MPG.121a9051179120fa989e2c@nntp.hpl.hp.com>

In article <x7zozz9fy8.fsf@home.sysarch.com> on 10 Aug 1999 23:03:27 -
0400, Uri Guttman <uri@sysarch.com> says...
 ...
> so make sure what version of perl is on that machine with perl -v. and
> remove the perl4 and get a proper install of perl5.

`perl -v` will give the version of perl that is first in the user's 
path.  What one really should try is `/path/to/perl -v` (using whatever 
is actually on the #! line).

But one can be confident that it is version 4, in advance of this test.

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Wed, 11 Aug 1999 00:22:24 -0400
From: rjk@linguist.dartmouth.edu (Ronald J Kimball)
Subject: Re: Help - Split Function Blowing My Mind Away!!
Message-Id: <1dwchml.ieaun8nk3m7aN@p144.tc19a.metro.ma.tiac.com>

Larry Rosler <lr@hpl.hp.com> wrote:

> [Posted and a courtesy copy mailed.]
> 
> In article <37b076fd$0$225@nntp1.ba.best.com> on 10 Aug 1999 19:01:17
> GMT, John Callender <jbc@shell2.la.best.com> says...
> > Larry Rosler <lr@hpl.hp.com> wrote:
> > 
> > > And all will be well.  (This is on my Top Ten list of Perl surprises for
> > > beginners.  The vertical bar is a popular field separator!)
> > 
> > If you really have such a list, I'd be very interested in seeing it.
> 
> Well, I'll show them, in no particular order.  I'm sure others will be
> able to slice and dice, transmogrify and improve.

> [snip]

That looked more like a list of Top Ten beginner mistakes, and
split(/|/) wasn't even on it.  Where's the list of Top Ten Perl
surprises for beginners?  :)

-- 
 _ / '  _      /       - aka -
( /)//)//)(//)/(   Ronald J Kimball      rjk@linguist.dartmouth.edu
    /                                http://www.tiac.net/users/chipmunk/
        "It's funny 'cause it's true ... and vice versa."


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

Date: 11 Aug 1999 03:09:21 GMT
From: sholden@pgrad.cs.usyd.edu.au (Sam Holden)
Subject: Re: Improving speed of a sub
Message-Id: <slrn7r1qbq.rel.sholden@pgrad.cs.usyd.edu.au>

On Tue, 10 Aug 1999 22:33:56 -0400, Duncan Hill <dhill@sunbeach.net> wrote:
>
>Urm.. ok, I may be a twit here, but wouldn't that try and read the
>entire file into a scalar?  Granted, memory isn't too much of an issue
>if one person hits it, but if several hundred people hit the same part
>at once, memory is gonna hurt.

But surely a copyright notice is reasonably small anyway maybe a few hundred K.

-- 
Sam

Perl was designed to be a mess (though in the nicest of possible ways). 
	--Larry Wall


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

Date: Wed, 11 Aug 1999 00:41:32 -0400
From: brian@pm.org (brian d foy)
Subject: Re: Improving speed of a sub
Message-Id: <brian-ya02408000R1108990041320001@news.panix.com>

In article <Pine.LNX.4.10.9908102231260.19529-100000@bajan.pct.edu>, Duncan Hill <dhill@sunbeach.net> posted:

> On Tue, 10 Aug 1999, brian d foy wrote:
> 
> > In article <Pine.LNX.4.10.9908102153170.19395-100000@bajan.pct.edu>, 
> > Duncan Hill <dhill@sunbeach.net> posted:
> > >         while (<COPYRIGHT>)
> > >         {
> > >                 s/referrer/$foo/;
> > >                 print;
> > >         }
> 
> >    local $/ = undef;
> >    my $data = <FILE>;
> >    $data =~ s/this/that/g;
> >    print $data;
> 
> Urm.. ok, I may be a twit here, but wouldn't that try and read the
> entire file into a scalar?  Granted, memory isn't too much of an issue
> if one person hits it, but if several hundred people hit the same part
> at once, memory is gonna hurt.

do you want speed or memory?  choose one (1).

-- 
brian d foy                    
CGI Meta FAQ <URL:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>
Perl Monger Hats! <URL:http://www.pm.org/clothing.shtml>


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

Date: 10 Aug 1999 23:37:04 -0500
From: abigail@delanet.com (Abigail)
Subject: Re: list length
Message-Id: <slrn7r1vev.cat.abigail@alexandra.delanet.com>

Gerhard Muth (gmuth@bytecare.com) wrote on MMCLXX September MCMXCIII in
<URL:news:37B04348.EFE9315D@caci.co.uk>:
() 
() Is there a way to get the number of elements of a list WITHOUT 
() assigning it to a variable?

Yes. Use a looping construct and a variable. But why the variablefobia?

() e.g. Thread->list gives a list of Threads. This works fine but is
() not what I want:
() 
() @l = Thread->list;
() print $#l." threads in list";

Well, that would be wrong.

    print @l."threads in a list";

But why is that so bad?

() I looked to a lot documentation as well as faqs but could not
() find a method or function to get the number of elems of the list.

There isn't a buildin function that does that.

If you want to find out the numbers, either use a looping construct
and a counter, or put it in an array and evaluate the array in
scalar context.

() I would expect something like Thread->list->length or
() length(Thread->list)

Nope. You can easily write a sub that returns the appropriate number,
but then, things would be assigned to @_, and for some odd reason,
you are scared of variables.

But this works:

    do {my $c; map {$c ++} Thread -> list; $c};



Abigail
-- 
sub f{sprintf$_[0],$_[1],$_[2]}print f('%c%s',74,f('%c%s',117,f('%c%s',115,f(
'%c%s',116,f('%c%s',32,f('%c%s',97,f('%c%s',0x6e,f('%c%s',111,f('%c%s',116,f(
'%c%s',104,f('%c%s',0x65,f('%c%s',114,f('%c%s',32,f('%c%s',80,f('%c%s',101,f(
'%c%s',114,f('%c%s',0x6c,f('%c%s',32,f('%c%s',0x48,f('%c%s',97,f('%c%s',99,f(
'%c%s',107,f('%c%s',101,f('%c%s',114,f('%c%s',10,)))))))))))))))))))))))))


  -----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including  Dedicated  Binaries Servers ==-----


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

Date: 10 Aug 1999 23:42:21 -0500
From: abigail@delanet.com (Abigail)
Subject: Re: list length
Message-Id: <slrn7r1vot.cat.abigail@alexandra.delanet.com>

Derek Sherlock (derek_sherlock@hp.com) wrote on MMCLXX September MCMXCIII
in <URL:news:rr0jb3.7hpqa2@bogomip.fc.hp.com>:
`` 
`` Hi,
`` 
`` If you access a list in a scalar context, you get the number of elements.  In
`` context, you get the elements themselves.

No you don't.

   $foo = ('aap', 'noot', 'mies');
   print "I think \$foo is 3, but it turns out that \$foo eq '$foo'";


`` You can force a scalar context with the "scalar" operator if necessary.
`` 
`` For example:
`` 
`` @a = qw(aa ab ac q1 q2 q3);
`` print scalar grep m/a./,@a;
`` print "\n";                                          
`` 
`` The print operator provides a list context to its expression.  The scalar ope
`` coeerces it into a scalar context.  The grep returns a 3 element list which, 
`` scalar context, evalutaes to the number 3.


Yes, but that's a property of _grep_, not of the list.

Of do you think that:

    print "Today is " . localtime;

prints:

    Today is 9

?



Your lines are way too long. Stop that.



Abigail
-- 
perl -we 'print split /(?=(.*))/s => "Just another Perl Hacker\n";'


  -----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including  Dedicated  Binaries Servers ==-----


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

Date: Wed, 11 Aug 1999 03:10:00 GMT
From: mgjv@comdyn.com.au (Martien Verbruggen)
Subject: Re: Need help using OO Perl
Message-Id: <cQ5s3.152$O21.8288@nsw.nnrp.telstra.net>

In article <7oqkcv$mr1$1@news.quebectel.com>,
	"Benjamin Bérubé" <berube@odyssee.net> writes:

A good source of information about perl OO are the following documents:

# perldoc perlref
# perldoc perlmod
# perldoc perlobj
# perldoc perlbot

and most importantly

# perldoc perltoot

> package MyClass;
> 
> sub new
> {
>  my($myref) = {};
>  $myref->{'counter'} = 0; # initialize counter element
>  $myref->{'array'} = [];  # create anonymous array
>  bless($myref);
>  return ($myref);
> }

While this will work, it is cleaner, and safer, to do something like:

sub new
{
    my $proto = shift;      # Get prototype
    my $class = ref($proto) || $proto; # class is either what this proto
                                       # refers to, or the proto itself

    my($myref) = {};
    $myref->{'counter'} = 0; # initialize counter element
    $myref->{'array'} = [];  # create anonymous array

    # It's much better to use the two-argument form of bless. It makes
    # inheritance possible
    bless $myref, $class;
    return $myref;
}

General comment: it's probably better to only use brackets when you
want to use them. Otherwise you may be putting things in list context
when you really don't want that.

> 
> sub add
> {
>  my($object) = shift;
> 
>  # this will write "titi" in the array at the position of the counter, then
> increment the counter

Actually, it increments the counter first. Then it places the element
in the array in the position that the counter was on just before the
increment. And it writes "titi".$counter, not just "titi". Comments
should reflect the actual code, not contradict it.

pedantry, but still..

>  local($counter) = $object->{'counter'}++;
>  ${$object->{'array'}}[$counter] = "titi".$counter;
> }

> (1) What means using ${} as in the following line ?
>   ${$object->{'array'}}[$counter] = "titi".$counter;

$object->{'array'} contains a reference to an array. The block
{$object->{'array'}} returns that reference. Then you use that
reference the same way you would use a reference to an array:

$ref = \@array;
$$ref[$i] = 'foo';
${$ref}[$i] = 'foo';

or, in your case:

${$object->{'array'}}[$counter];

You need to use a block in this case, because of precedence.

I personally prefer

$object->{'array'}->[$counter];

The perlref page explains these.

> (2) How can I use some sort of variable that would be a pointer to
> $object->{'counter'} (for example) so that I could do :
>   $pointer = 4; # assuming that $pointer points to the counter of the
> current object
> This way, if I would do :
>   print $object->{'counter'};
> I would get "4".

Euhmmm.. it really breaks OO design if you do that, but here's a way:

$a->{'counter'} = 4;

my $counter_ref = \$a->{'counter'};
$$counter_ref = 10:

> (3) Same question as before, but a pointer to the $object->{'array'} so that
> I could do :
>   print @array;
> or
>   $array[$pos] = "blabla";

Again, this breaks OO design. You should provide accessor methods for
this. Allowing people to access your private data directly makes it
certain that you can never change the internal implementation of your
class.

Since the array is already a reference:

my $a_ref = $a->{'array'};


> For both question two and three, I read something about using the * like in
> *pointer, but i'm not getting how it works.

Don't do that. :) Don't use typeglobs.

Martien
-- 
Martien Verbruggen                  | 
Interactive Media Division          | That's funny, that plane's dustin'
Commercial Dynamics Pty. Ltd.       | crops where there ain't no crops.
NSW, Australia                      | 


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

Date: 10 Aug 1999 22:58:39 -0500
From: abigail@delanet.com (Abigail)
Subject: Re: perl script - help
Message-Id: <slrn7r1t6v.cat.abigail@alexandra.delanet.com>

Larry Rosler (lr@hpl.hp.com) wrote on MMCLXX September MCMXCIII in
<URL:news:MPG.121a083e4e30e343989e1a@nntp.hpl.hp.com>:
() 
() %_ shares the same typeglob as $_ and @_, thus it is implicitly global 
() as they are.  But it has no special semantics, so it is available 
() without declaration as an obscurely named hash -- just fine for 
() Obfuscated Perl.  But now too many people wil know about it, so the veil 
() of obscurity is lifting.  Sigh...


Interesting that you mention this. I just wrote the following.


=pod

=head1 THE MAKING OF A JAPH

=head2 Introduction

Some time ago, I was reading I<alt.sysadmin.recovery>, without paying much
attention. At one moment, a message [1] comes by, and of that message,
all I see is I<"&%%hole">, and I think by myself, that is an odd way of
calling a sub. Minutes later, I wonder, can you do that in Perl? Some
experimenting shows you can, and with a half an hour of fiddling, a new
JAPH is born.

=head2 Scalar %hash.

Let's first make a hash, and a sub that prints out the famous words.
Since we already have a hash, our sub will make use of the hash to
print its message. Observing that C<"Just" lt "Perl">, we end up with

    %_ = (Just => another => Perl => Hacker);
    sub {foreach (sort keys %_) {print "$_ $_{$_} "}};

Not much obscurity going on here, except the bare word I<Hacker>,
and the unusual name C<%_>.

The next step will be to find out what C<%_> in scalar context is.
A simple Perl program will tell us.

    perl -le '%_ = (Just => another => Perl => Hacker); print scalar %_'

That will print C<2/8>, meaning 2 filled buckets out of a total of 8
buckets. But its meaning isn't relevant. It's the C<2/8> that is.

=head2 Creating a sub

Now that we know the value of our hash in scalar context, we can create
a sub by that name using a typeglob:

    *{"2/8"} = sub {foreach (sort keys %_) {print "$_ $_{$_} "}};
    %_ = (Just => another => Perl => Hacker); &{%_};

The code above will print "Just another Perl Hacker ", but it only has
one C<%> in the subroutine call. But to use two C<%>'s, all we need to
do is repeat the trick:

    *{"2/8"} = sub {foreach (sort keys %_) {print "$_ $_{$_} "}};
    %{"2/8"} = %_ = (Just => another => Perl => Hacker); &{%{%_}};

By creating a hash with the name C<2/8>, whose value in scalar context
is the same as its name, we could even repeat this trick forever.
But our initial goal was to have two C<%>'s after the C<&>.

=head2 Further obfuscation.

Having the C<"2/8"> in the typeglob, and as the name for the hash, it makes
it very easy to guess what is going on. So, this needs a little more work.
What can we do with C<"2/8">? Well, it looks a bit like C<28>. And C<28>
has a nice property, it is the sum of the first 7 positive integers. We
use an C<eval>, a C<join>, and a range to create C<28>:

    eval join "+" => 1 .. 7;

But that's only C<28>, not C<"2/8">. There are of course several ways to
go from the one to the other. One of the ways is splitting C<28> in its
separate numbers, and stringifying the resulting array with C<$" = "/">.
Now, C<split> in scalar context, implicitely splits into the C<@_> array,
which makes it a bit more obscure, but fits nicely with our use of C<%_>
and C<$_>.

That leads to:

    $" = "/"; split // => eval join "+" => 1 .. 7;
    *{"@_"} = sub {foreach (sort keys %_)  {print "$_ $_{$_} "}};
    %{"@_"} = %_ = (Just => another => Perl => Hacker); &{%{%_}};

=head2 The finishing touch.

The C<$"> above the C<*{"@_"}> and C<%{"@_"}> just doesn't look nice.
Wouldn't C<${"@_"}> be a lot nicer? Well, we have an extra line to play
with (obfuscated Perl just isn't obfuscated if it needs more than 4x80
characters, whatever whimpy rules the obfuscated Perl contests like to
come up with). Repeating the C<split> in scalar context trick, and lining
things up, we arrive at:

                   split // => '"';
    ${"@_"} = "/"; split // => eval join "+" => 1 .. 7;
    *{"@_"} = sub {foreach (sort keys %_)  {print "$_ $_{$_} "}};
    %{"@_"} = %_ = (Just => another => Perl => Hacker); &{%{%_}};

The added benefit is that it throws off the people who think that
C<${"@_"}> and C<%{"@_"}> are both entries in the same glob; they are 
not, as C<@_> changes.

=head2 References

  [1]  ``However, and I think this is localized to near USAnia, but the name
         we associate with American Indian tribes is nearly always "&%%hole"
         or the like in the language of the tribe to the East, whom explorers
         had to meet before making it there.''
      David Jacoby in I<Sea Doo recovery?...>.
      I<E<lt>URL:news:7o8i85$jlb$1@mozo.cc.purdue.eduE<gt>>. 4 Aug 1999.


=head2 Author

This article was written by I<Just another Perl Hacker> on 10 Aug 1999.

=head2 Copyright

The text and the code in this article are in the Public Domain.
Use it wisely.

=cut


Abigail
-- 



  -----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including  Dedicated  Binaries Servers ==-----


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

Date: 10 Aug 1999 23:07:17 -0500
From: abigail@delanet.com (Abigail)
Subject: Re: pricing a perl job
Message-Id: <slrn7r1tn5.cat.abigail@alexandra.delanet.com>

Elaine -HFB- Ashton (elaine@chaos.wustl.edu) wrote on MMCLXX September
MCMXCIII in <URL:news:37B0A538.6E477CAC@chaos.wustl.edu>:
$$ 
$$ Good lord, now you go after his sense of style. I go by either
$$ HFB,

Sound like a very useful, but highly toxic chemical. Patented by Bayer
or BASF.

$$ HappyFunBall

Bouncy. Very bouncy.

$$ or e.

Short. To the point. Makes me think of IBM.



Abigail
-- 
The one and only.


  -----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including  Dedicated  Binaries Servers ==-----


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

Date: 11 Aug 1999 04:50:11 GMT
From: dha@panix7.panix.com (David H. Adler)
Subject: Re: pricing a perl job
Message-Id: <slrn7r2082.mef.dha@panix7.panix.com>

In article <100819991245455752%max@maxgraphic.com>, Max Pinton wrote:
>
>So, despite your efforts, I do have what I came for: a rough idea of
>what to charge for the work I've done. Feel free to return to flaming
>other posters you don't feel are worthy of answers to their simple
>questions.

Hm.  And people wonder why some clpmisc denizens get fed up...

No offence, but if you thought that brian's response was a flame, you
must lead a fairly sheltered life.

The advice brian gave was quite sound, and could very well help you,
perhaps not with your literal query, but with the more general case
which, assuming you continue to charge for Perl programming, will be
more useful.  I certainly hope you don't intend to come and ask that
question everytime the issue comes up for you... :-/

It seems to me that brian was giving you *more* than you asked for,
rather than less.  You are, by your own admission, new at this.  The
amount a seasoned programmer would charge for a service (not to
mention how it would break down timewise) really doesn't have any
relation to what it would be "fair" (I believe I'm using your own term
here..) for you to charge.

I'm probably going on for no good purpose, though, as it seems that
some people are going to take things as flames, regardless of their
intent or usefulness.

wearily,

dha

-- 
David H. Adler - <dha@panix.com> - http://www.panix.com/~dha/
Uh, yeah.  Well, that's my mistake for the year...
	- Larry Wall


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

Date: 11 Aug 1999 03:09:29 GMT
From: damian@cs.monash.edu.au (Damian Conway)
Subject: Re: reference to object method
Message-Id: <7oqph9$ij0$1@towncrier.cc.monash.edu.au>

"David Christensen" <dchristensen@california.com> writes:

>Damian:

>Approach #1 doesn't seem to work:

Works under 5.005_02 or later.
What version are you running?

Damian


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

Date: Wed, 11 Aug 1999 03:02:25 GMT
From: Jason Smith <jsmith19991@my-deja.com>
Subject: Script Problem
Message-Id: <7oqp3t$dor$1@nnrp1.deja.com>

I'm having a problem with a perl script. The problem
we are having is with the E-mailing of information to a
client. To make it simple, the script that sends an Email
to someone when they enter their address (Mary Smith in
the following example). The script includes the following
lines for sending the mail:

 ...

open(MAIL,"|$mailprog -t");

print MAIL "To: Mary Smith\n";
print MAIL "From: John\n\n");
print MAIL "Subject: Information\n\n";

 ...

When the Email is received by the person however, the "From"
field reads not "John", but "John@whsun626.webhosting.com"

How do we get rid of the extra server info following the name?
We've tried a few things but it always reads the same.

Thank you for your time...


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.


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

Date: Wed, 11 Aug 1999 03:49:30 GMT
From: mgjv@comdyn.com.au (Martien Verbruggen)
Subject: Re: Script Problem
Message-Id: <ep6s3.171$O21.9110@nsw.nnrp.telstra.net>

In article <7oqp3t$dor$1@nnrp1.deja.com>,
	Jason Smith <jsmith19991@my-deja.com> writes:

You again? This is the third time that you post this question. It's
off-topic, and it has been answered. Why don't you read the answers?

You've just made it clear that you don't want to be read, so I'll make
sure that I won't read anything you post again.

*plonk*

Martien
-- 
Martien Verbruggen                  | 
Interactive Media Division          | Begin at the beginning and go on till
Commercial Dynamics Pty. Ltd.       | you come to the end; then stop.
NSW, Australia                      | 


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

Date: Tue, 10 Aug 1999 20:56:23 -0700
From: moseley@best.com (Bill Moseley)
Subject: Re: Script Problem
Message-Id: <MPG.121a944652b8184f9896a4@nntp1.ba.best.com>

Jason Smith (jsmith19991@my-deja.com) seems to say...
> open(MAIL,"|$mailprog -t");
> print MAIL "From: John\n\n");
> 
> When the Email is received by the person however, the "From"
> field reads not "John", but "John@whsun626.webhosting.com"
> 
> How do we get rid of the extra server info following the name?
> We've tried a few things but it always reads the same.
> 

I'd think man $mailprog would be the answer.  But since you are looking 
for a perl solution:

s[\@whsun626\.webhosting\.com][];


> Sent via Deja.com http://www.deja.com/

Don't people have usenet access anymore?  Or is it better to see lots of 
ads in color than risk receiving junk mail?



-- 
Bill Moseley mailto:moseley@best.com
add 'nospam' to my email to make it easy for the spiders


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

Date: Tue, 10 Aug 1999 22:36:45 -0500
From: seong joon bae <seongbae@students.uiuc.edu>
Subject: Some error I can't fix...need help please.
Message-Id: <Pine.GSO.4.10.9908102234370.6004-100000@ux9.cso.uiuc.edu>


#!/usr/bin/perl

use Net::Ping;
$host="www.perl.com";
$p = Net::Ping->new();
print "$host is alive.\n" if $p->ping($host);
$p->close();

When I run above script, I get this error:

Bad arg length for Socket::unpack_sockaddr_in, length is 0, should be 16
at /usr/lib/perl5/i386-linux/5.00401/Socket.pm line 249.

Can anyone tell me what's wrong?
The ping script can't be any simpler than that...
Thanks.





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

Date: Tue, 10 Aug 1999 23:18:11 -0400
From: "Benjamin Bérubé" <berube@odyssee.net>
Subject: Re: Strange File Behavior
Message-Id: <7oqptr$qoh$1@news.quebectel.com>

You might try using the chomp function to remove any cariage returns like
this :

for ( $i=1; $i<=50; $i++ )
{
 chomp ($theArray[ $i ]);        # line added to remove any returns.
 print theFile ( "$theArray[ $i ]\n" );
}

Hope this might help.

Ben


John J. Straumann a écrit dans le message
<37B0E2D7.8732DD5C@worldnet.att.net>...
>Hey All:
>
>I am writing data out to a file via a CGI. each time the CGi runs, i
>want the contents of the file overwritten with the new data from the
>CGI. The file is a simple sequential file of numbers, like this:
> ...
> the file is created and the data written out with this line:
>   for ( $i=1; $i<=50; $i++ )
>   {
>    print theFile ( "$theArray[ $i ]\n" );    # Write to file
>   }
>This code gives me the file as shown above, one number per line.
>However, if the program runs again, and the file is opened using the
>same command, the contents are not wiped out and replaced, but the
>program adds a newline on each line, leaving this:





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

Date: Wed, 11 Aug 1999 03:52:32 GMT
From: mgjv@comdyn.com.au (Martien Verbruggen)
Subject: Re: Strange File Behavior
Message-Id: <4s6s3.173$O21.9110@nsw.nnrp.telstra.net>

In article <37B0E2D7.8732DD5C@worldnet.att.net>,
	"John J. Straumann" <jstraumann@worldnet.att.net> writes:
> Now, the program works fine when the file DNE. Using 

Don't abbreviate that. Just type 'does not exist'. It's not in my
dictionary.

> if ( open( theFile, ">$dataFile" ) )         # Create File
> 
> the file is created and the data written out with this line:
>    for ( $i=1; $i<=50; $i++ )
>    {
>     print theFile ( "$theArray[ $i ]\n" );    # Write to file	
>    } 
> This code gives me the file as shown above, one number per line.
> However, if the program runs again, and the file is opened using the
> same command, the contents are not wiped out and replaced, but the
> program adds a newline on each line, leaving this:

Where did @theArray come from? Did you read that from the file? Did
you remember to get rid of the newlines that you read?

Martien
-- 
Martien Verbruggen                  | 
Interactive Media Division          | For heaven's sake, don't TRY to be
Commercial Dynamics Pty. Ltd.       | cynical. It's perfectly easy to be
NSW, Australia                      | cynical.


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

Date: Tue, 10 Aug 1999 21:23:19 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Strange File Behavior
Message-Id: <MPG.121a9a8a8c3430ec989e2d@nntp.hpl.hp.com>

[Posted and a courtesy copy sent.]

In article <37B0E2D7.8732DD5C@worldnet.att.net> on Tue, 10 Aug 1999 
22:41:27 -0400, John J. Straumann <jstraumann@worldnet.att.net> says...
> I am writing data out to a file via a CGI.

CGI is a protocol.  Oh, you mean a CGI program.  :=)

>                                            each time the CGi runs, i
> want the contents of the file overwritten with the new data from the
> CGI. The file is a simple sequential file of numbers, like this:
> 0
> 4
> 5
> etc.
> 
> Now, the program works fine when the file DNE.
                                            ^^^

My crystal ball translated that to Does Not Exist.  That means that 
there are no newlines in the array -- the first time the program is run.

>                                                   Using 
> if ( open( theFile, ">$dataFile" ) )         # Create File

By Perl convention, filehandles are written in all upper-case letters.  
But you can ignore the convention if you wish.  It just makes the 
programs harder for experienced Perl programmers to read.

> the file is created and the data written out with this line:
>    for ( $i=1; $i<=50; $i++ )
>    {
>     print theFile ( "$theArray[ $i ]\n" );    # Write to file	
>    }

Is there something special about the initial element of the array, that 
you choose to ignore it in printing the array?  And how do you know that 
there are at least 51 elements in the array, because you print that many 
without apparently checking the size of the array.
 
> This code gives me the file as shown above, one number per line.
> However, if the program runs again, and the file is opened using the
> same command, the contents are not wiped out and replaced, but the
> program adds a newline on each line, leaving this:
> 1
> 
> 2
> 
> 4
> 
> 6
> 
> etc.

No.  The contents are replaced, by what you show: each number followed 
by two newlines.

> I even tried reading the file into an array, deleting the file,
> processing, and then re-creating the file and writing the new data out,
> but no dice.

When you read the file into an array, each element ('line') ends in a 
newline.  When you write the data out again, you are appending a second 
newline to each element.

     for ( $i = 1; $i <= 50; $i++ )
     {
         print theFile $theArray[ $i ];    # Write to file	
     } 

I'll rewrite that in Perl, instead of C, and include a length check.

     print THE_FILE @theArray[1 .. ($#theArray > 50 ? 50 : $#theArray)];

But I'll bet you want to start printing at index 0!

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Tue, 10 Aug 1999 20:12:11 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: turn $6 into $6000
Message-Id: <MPG.121a89d6a723ef6c989e29@nntp.hpl.hp.com>

In article <37B0E571.AC227A25@erols.com> on Tue, 10 Aug 1999 22:52:33 -
0400, Matthew O. Persico <mpersico@erols.com> says...
> $x='$6';
> $x += '000';
> print $x;

Did you really mean '+='?  It works fine with '.='.

But is this thread still of any interest?

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: 10 Aug 1999 23:18:14 -0400
From: Uri Guttman <uri@sysarch.com>
Subject: Re: turn $6 into $6000
Message-Id: <x7so5r9f9l.fsf@home.sysarch.com>

>>>>> "IRADA" == I R A Darth Aggie <fl_aggie@thepentagon.com> writes:

  IRADA> On 10 Aug 1999 19:33:12 -0400, Uri Guttman <uri@sysarch.com>, in
  IRADA> <x73dxrb493.fsf@home.sysarch.com> wrote:

  IRADA> + yeah, prove it, you canadian MF!!

  IRADA> You mis-spelt UF, you PF.

i meant MF, i slightly forgot about UF. that was a great song!!

  IRADA> Watch it, Uri, or we'll install a V-chip in you. It'll work on various
  IRADA> words like 'Microsoft', 'Bill Gates', 'Windows', 'NT', and 'python'.

i spell those 'u$hit', 'uncle bill', 'winblows/windoze', '' and 'monty'

uri

-- 
Uri Guttman  -----------------  SYStems ARCHitecture and Software Engineering
uri@sysarch.com  ---------------------------  Perl, Internet, UNIX Consulting
Have Perl, Will Travel  -----------------------------  http://www.sysarch.com
The Best Search Engine on the Net -------------  http://www.northernlight.com
"F**king Windows 98", said the general in South Park before shooting Bill.


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

Date: 1 Jul 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 1 Jul 99)
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" from
almanac@ruby.oce.orst.edu. The real FAQ, as it appeared last in the
newsgroup, can be retrieved with the request "send perl-users FAQ" from
almanac@ruby.oce.orst.edu. 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" from
almanac@ruby.oce.orst.edu. 

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 V9 Issue 456
*************************************


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