[23934] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6135 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Feb 15 06:05:42 2004

Date: Sun, 15 Feb 2004 03:05:07 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 15 Feb 2004     Volume: 10 Number: 6135

Today's topics:
        ActiveState Perl Haiku Contest Results announced (Himanshu Garg)
        Choosing grouping of split items <BLOCKSPAMfishfry@your-mailbox.com>
    Re: Choosing grouping of split items <Juha.Laiho@iki.fi>
        Environment question <none@none.com>
    Re: Environment question <tadmc@augustmail.com>
    Re: Environment question <uri@stemsystems.com>
    Re: Environment question <uri@stemsystems.com>
    Re: How to call a perl script that returns nothing in a (John W.)
    Re: perl and db-module <none@nowhere.non>
    Re: Problem opening sed pipe <me@privacy.net>
    Re: Problems with version 5.8.0 on OS/2 <bart.lateur@pandora.be>
    Re: Problems with version 5.8.0 on OS/2 <nospam-abuse@ilyaz.org>
        read/write to a character device (Stoyan Stoyanov)
    Re: Save the Bandwidth -smarter way to send the same fi (KK)
    Re: Save the Bandwidth -smarter way to send the same fi <kirk@strauser.com>
    Re: Sending HASH over TCP <sammie-nospam@greatergreen.com>
        Understanding 'scope' (Prabh)
    Re: Understanding 'scope' <usenet@morrow.me.uk>
    Re: Understanding 'scope' <dwall@fastmail.fm>
    Re: Understanding 'scope' <tadmc@augustmail.com>
    Re: Understanding 'scope' <uri@stemsystems.com>
    Re: XML::Simple array size <usenet@morrow.me.uk>
    Re: XML::Simple array size <tadmc@augustmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 14 Feb 2004 19:45:01 -0800
From: himanshu@gdit.iiit.net (Himanshu Garg)
Subject: ActiveState Perl Haiku Contest Results announced
Message-Id: <a46e54c7.0402141945.143464e3@posting.google.com>

Hello,
    
    Please don't mind my posting it here. I was delighted to see the
Haiku out there. Visit the following website ( if you haven't already
) to see the award winning poetry about perl.

    http://aspn.activestate.com/ASPN/Perl/Haiku/AboutPerl
    
    I hope 
    you too, 
    will enjoy those.

Thank You
++imanshu.


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

Date: Sun, 15 Feb 2004 08:29:25 GMT
From: fishfry <BLOCKSPAMfishfry@your-mailbox.com>
Subject: Choosing grouping of split items
Message-Id: <BLOCKSPAMfishfry-A6B8C9.00292615022004@netnews.comcast.net>

If I have 

  $text = 'abc - def  - ghi -  jkl";

then 

    @tokens = split(/-/, $text, 3);

splits the line into abc, def, ghi - jkl (ignoring leading and trailing 
spaces).

I have an application where there may be 3 or 4 items separated by '-' 
but if there are 4, I want them returned as abc - def, ghi, and jkl.

Is there a one-liner for that?


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

Date: Sun, 15 Feb 2004 09:27:00 GMT
From: Juha Laiho <Juha.Laiho@iki.fi>
Subject: Re: Choosing grouping of split items
Message-Id: <c0ndsf$8e2$1@ichaos.ichaos-int>

fishfry <BLOCKSPAMfishfry@your-mailbox.com> said:
>If I have 
>
>  $text = 'abc - def  - ghi -  jkl";
>
>then 
>
>    @tokens = split(/-/, $text, 3);
>
>splits the line into abc, def, ghi - jkl (ignoring leading and trailing 
>spaces).
>
>I have an application where there may be 3 or 4 items separated by '-' 
>but if there are 4, I want them returned as abc - def, ghi, and jkl.
>
>Is there a one-liner for that?

I don't understand the magic of one-lining "everything"; write code as
it fits nicely. Of course, Perl golf is a different issue (writing code
to accomplish a given task with a minimal amount of characters).

As to how I'd solve your problem would be split the data, then join
the first two fields if the split resulted in 4 fields. Alternative
would be to write a regular expression that always returns 3 fields;
something like
/(.*)\s+-\s+([^-]*)\s+-\s+(.*)/
might be close to your needs. Whether this is faster or slower than
splitting and recombining I don't know -- but that you can find out
with the benchmarking modules.
-- 
Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
         PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)


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

Date: Sat, 14 Feb 2004 19:44:46 -0600
From: "Bill Staples" <none@none.com>
Subject: Environment question
Message-Id: <pan.2004.02.15.01.44.44.642120@none.com>

I have been using perl for some time but only as called modules from other
programs, so the perl sources have been fairly small - usually not much
more than a page and most of the time only a few lines.

I am wanting to do a much larger project totally in perl which brings up
the question.  Do heavy perl coders make just one huge source file?  

In my other languages, Kylix, Delphi, C, each class, module, object,
whatever, is in its own source file so that you are only looking at the
needed code at the moment.  Since Perl apparently doesn't have an include
statment or equivilent, what the heck do you use to keep from having to
scroll up and down looking for a subroutine in 47 pages of code?

It would be easy enough to make a script to merge different sources and
then hand them to perl but that seems to be a kludge and there must be a
different answer - or is there?

Thanks
Bill


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

Date: Sat, 14 Feb 2004 21:50:10 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Environment question
Message-Id: <slrnc2tr3i.45f.tadmc@magna.augustmail.com>

Bill Staples <none@none.com> wrote:

> From: "Bill Staples" <none@none.com>
                        ^^^^^^^^^^^^^
                        ^^^^^^^^^^^^^

There have been some silly people posting from none@none.com
here in the past. Are you willing to take on the reputation
that they have built as your own reputation?

More people will see your articles if you use some other (ie. proper)
form of address munging...


> I have been using perl for some time but only as called modules 


I think you must mean "called programs", as "modules" appears
to be exactly the thing that you are looking for below.


> Do heavy perl coders make just one huge source file?  


No, and the heavy Perl coders don't either.


> Since Perl apparently 


Where is it that you have looked for it?


> doesn't have an include
> statment or equivilent,


Yes it does, they are called "modules":

   perldoc -f use          (nice)
   perldoc -f require      (crufty)

   perldoc perlmod
   perldoc perlmodlib
   perldoc perlmodinstall
   perldoc perlmodstyle


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


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

Date: Sun, 15 Feb 2004 04:15:41 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Environment question
Message-Id: <x7d68h9dw3.fsf@mail.sysarch.com>

>>>>> "BS" == Bill Staples <none@none.com> writes:

  BS> In my other languages, Kylix, Delphi, C, each class, module, object,
  BS> whatever, is in its own source file so that you are only looking at the
  BS> needed code at the moment.  Since Perl apparently doesn't have an include
  BS> statment or equivilent, what the heck do you use to keep from having to
  BS> scroll up and down looking for a subroutine in 47 pages of code?

no include statement? where did you learn perl? perl has at least 3
include-like operations and eval which means you can roll your own as
well. and do you think all the massive amounts of perl code written is
only in large files? and what about cpan?

  BS> It would be easy enough to make a script to merge different
  BS> sources and then hand them to perl but that seems to be a kludge
  BS> and there must be a different answer - or is there?

please be learning more perl before making wrong statements like this.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: Sun, 15 Feb 2004 04:16:59 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Environment question
Message-Id: <x7ad3l9dtx.fsf@mail.sysarch.com>

>>>>> "TM" == Tad McClellan <tadmc@augustmail.com> writes:

  TM> Yes it does, they are called "modules":

  TM>    perldoc -f use          (nice)

compile time

  TM>    perldoc -f require      (crufty)

run time.

both are nice. both have their niches.

and you forgot do 'file'! THAT is crufty!

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: 14 Feb 2004 18:37:52 -0800
From: jwagenleitner@yahoo.com (John W.)
Subject: Re: How to call a perl script that returns nothing in an HTML
Message-Id: <a4c755ee.0402141837.7f6e7b5b@posting.google.com>

"Danny" <dannywork5@hotmail.com> wrote in message news:<VVlWb.13289$rv1.6599402@news4.srv.hcvlny.cv.net>...
> Newbie here
> Thanks for the help with this command:
> 
> print "Status: 204 No Content\n\n";
> which returns nothing in a perl script.
> 
> This is great because I just want my perl script to increment a counter 
> and put result in a text file which it does fine how.
> But now how do I call this from an .html?  I used to call it like this
> <img src="path to cgi script">
> this increments the counter but there is a little box where an actual 
> image should be, so how do I:
> 1. have the script return a bogus image (using below)
> 
> 
>       print "Content-type: image/gif\n\n";
>       (not sure how I would print a graphic here)
> 
> 
> 2. or have the call html file run this script somehow without using the 
> image tag so I dont get that little empty graphic display.
> 
> Thanks in advance
> 
> 
> 
> --

The following img tag should not display anything on the page and will
call the CGI as desired...

<img src="<cgi script>" height="0" width="0" border="0">


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

Date: Sun, 15 Feb 2004 00:12:35 -0500
From: non existent <none@nowhere.non>
Subject: Re: perl and db-module
Message-Id: <pan.2004.02.15.05.12.34.531768@nowhere.non>

On Fri, 13 Feb 2004 07:51:44 -0800, Dan Bent wrote:

> Can't load
> '/usr/local/lib/perl5/site_perl/5.8.3/i386-freebsd/auto/Cyrus/IMAP/IMAP.so'

Does IMAP.so exist in the directory:
/usr/local/lib/perl5/site_perl/5.8.3/i386-freebsd/auto/Cyrus/IMAP/ ?

Does /usr/local/lib/perl5/site_perl/5.8.3/i386-freebsd show up under @INC:
when executing 'perl -V' ?

If not then:
'export PERL5LIB=/usr/local/lib/perl5/site_perl/5.8.3/i386-freebsd' and
look at the output of 'perl -V' again. You should see the new path under
@INC: (maybe freeBSD uses a different command other than export - sorry
can't help you here) and IMAP.so should be found (if it is indeed there).

Chris


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

Date: Sun, 15 Feb 2004 15:17:16 +1300
From: "Tintin" <me@privacy.net>
Subject: Re: Problem opening sed pipe
Message-Id: <c0mku1$1936qq$1@ID-172104.news.uni-berlin.de>


"freewilly3d" <bill.standke@medica.com> wrote in message
news:24fefa25.0402121741.296c2737@posting.google.com...
> I'm trying to open the following pipe in perl open(SASPS,"ps -
> ef|sed 's/^\(.\{32\}\) /\1/; s/^ *$/*/'|grep sas| sort +0 -1 -d +7 -
> 8 -d +4 -5 -d +6 -7 -d |") and get a sed error returned. The command
> works fine at the command line. The error has something to do with
> th *$/ part of the code. This is the message I get back
> adwprod:/prod/rptg/bstandke >pssas
> sed: 0602-404 Function s/^(.{32}) //; s/^ * cannot be parsed.
> any suggestions?

Replace the first line in your script with

#!/bin/sh




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

Date: Sun, 15 Feb 2004 09:04:42 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Problems with version 5.8.0 on OS/2
Message-Id: <03du2011skpu6978ivttlrd9qb0f5hd612@4ax.com>

Ilya Zakharevich wrote:

>Unless this build is completely broken, this is just bullshit. 

No need to yell at me, I have nothing to do with all this. Just go and
see for yourself, 

	<http://hobbes.nmsu.edu/cgi-bin/h-browse?dir=/pub/os2/dev/perl>

Get one of the binary distributions, and look at the README. 

All I did was quote.

Now go yell at the person who made that distribution, if you must.

-- 
	Bart.


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

Date: Sun, 15 Feb 2004 09:12:50 +0000 (UTC)
From:  Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: Problems with version 5.8.0 on OS/2
Message-Id: <c0nd6i$23ee$1@agate.berkeley.edu>

[A complimentary Cc of this posting was sent to
Bart Lateur 
<bart.lateur@pandora.be>], who wrote in article <03du2011skpu6978ivttlrd9qb0f5hd612@4ax.com>:
> Ilya Zakharevich wrote:
> 
> >Unless this build is completely broken, this is just bullshit. 
> 
> No need to yell at me, I have nothing to do with all this.

Com'on, you call *this* yelling?!  Just you wait...  ;-)

Sorry if you took this as being a missile in your direction (although
now I understand how you got this impression ;-().  In fact it was not
an invective at all; consider it as a statement of the relation of the
statement of README to the reality (or what the reality should better
be).

> Just go and see for yourself,

Having written this code, and having written the documentation about
how things work, I'm not much interested.  ;-)

Yours,
Ilya


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

Date: 14 Feb 2004 22:09:09 -0800
From: stoyansbg@yahoo.com (Stoyan Stoyanov)
Subject: read/write to a character device
Message-Id: <e9b9e613.0402142209.5b724314@posting.google.com>

I'd like to send some binary data directly to my USB printer and get
the response. The sending of the data seems to pass OK but I have
problems getting the response. What happens is that randomly I get: no
response at all, the correct response, or the correct response twice.
I tried several different ways of opening filehandles to the device
but in all cases I get the same results. I must be missing something.

Here is one of the script variants that I tried:

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

my $DEVICE = '/dev/usb/lp0';
my $buffer;
my $response;
my $byte;

my @line = split(/\s+/,'00 00 00 1B 01 40 45 4A 4C 20 31 32 38 34 2E
34 0A 40 45 4A 4C 0A 40 45 4A 4C 0A');

# converting the data
foreach (@line) {
    $buffer .= sprintf ("%c", hex $_);
}

# debug: dumping to a file whatever is to be sent to the printer
open AA, "> buffer.txt";
print AA $buffer;
close AA;

sysopen(FH, $DEVICE, O_RDWR | O_EXCL) or die $!;
my $ofh = select(FH); $| = 1; select($ofh);

# sending to printer
syswrite (FH, $buffer, 27) or die $!;

# reading the response
while (sysread (FH, $byte, 1)==1) {
    $response .= $byte;
}

# debug: dumping the response to a file
open AA, "> response.txt";
print AA $response;
close AA;

close (FH);
--------------------

Thanks!
Stoyan


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

Date: 15 Feb 2004 00:17:08 -0800
From: kewlkarun@yahoo.com (KK)
Subject: Re: Save the Bandwidth -smarter way to send the same file attachment to N users?
Message-Id: <c8fd5039.0402150017.3ad275c0@posting.google.com>

Kirk & Mina, thank you for throwing light on this topic. There is one
thing that still bothers me. I dont need a password to access the mail
server (mail.cs.college.edu) where as, I 'must' supply a passwd to
access the attachment stored in the server (cs.college.edu). my perl
program does not contain the feature of incorporating the password.
Are you suggesting to incorporate this feature? If yes, How can I
achieve this?
Regards,
-KK
Kirk Strauser <kirk@strauser.com> wrote in message news:<87u11t80ed.fsf@strauser.com>...
> -----BEGIN xxx SIGNED MESSAGE-----
> Hash: SHA1
> 
> At 2004-02-14T23:01:03Z, Mina Naguib <spam@thecouch.homeip.net> writes:
> 
> > Email was not designed with file attachments in mind.  Every time you send
> > a binary attachment, it has to be ASCII-armoured which increases it's
> > size, often 2-3 times the original binary file size.
> 
> Almost never.  Base64 encoding uses a 7-bit ASCII character to represent 6
> bits from the attachment, so it has a 33% overhead.  Note that the resulting
> encoding is trivially compressible to nearly the original size; any
> intermediate storage or transfer mechanism that uses compression (such as
> TLS) will use almost no more space than the original file.
> 
> > HTTP and FTP are good ways to send files.  Put your file on such aq
> > server and include a simple link to it in your e-mail.
> 
> I disagree.  Why?
> 
>   1) When you send email, *you* control when you're going to use your
>      bandwidth.  If you sent out 10,000 links to a file on your server, and
>      everyone decides to follow the link at the same time, then you might be
>      in trouble.
> 
>   2) Modern MTAs merge transmitions to given domains.  If 2500 out of your
>      10,000 emails are to aol.com users, then your mailserver could
>      reasonably send exactly one mail to aol.com, and specify that it should
>      go to all 2500 recipients.  That mitigates the bandwidth factor
>      somewhat.
> 
>   3) People are lazy.  If you're trying to send, say, a monthly newsletter
>      in PDF form to your customers, then the odds of them opening an
>      attachment are *much* higher than them clicking on a link to open a
>      browser and visit a webpage.  Plus, if the file is on their mailserver
>      or their client, then they don't have to re-download it from you if
>      they want to view it again a month from now.
> 
>   4) When you sent email, you're making an *outbound* connection.  When you
>      host the files locally, you have to configure your firewall and
>      services to allow access to the files in question but nothing more.
>      From a security point of view, I'd much rather establish connections
>      than accept them.
> 
> FTP and HTTP obviously have their uses, but it's not fair to use the reason
> that "email was not designed" to recommend against doing exactly that.  It
> has its place.
> - -- 
> Kirk Strauser
> The Strauser Group
> Open. Solutions. Simple.
> http://www.strausergroup.com/
> -----BEGIN xxx SIGNATURE-----
> Version: GnuPG v1.2.4 (GNU/Linux)
> 
> iD8DBQFALuz+5sRg+Y0CpvERAowvAJ9nDaPI0SYP4WrxcQTfApR17KJ99QCfed87
> jjR+GguN+F2O6ASi60bSZMI=
> =v42d
> -----END PGP SIGNATURE-----


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

Date: Sun, 15 Feb 2004 03:55:07 GMT
From: Kirk Strauser <kirk@strauser.com>
Subject: Re: Save the Bandwidth -smarter way to send the same file attachment to N users?
Message-Id: <87u11t80ed.fsf@strauser.com>

=2D----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

At 2004-02-14T23:01:03Z, Mina Naguib <spam@thecouch.homeip.net> writes:

> Email was not designed with file attachments in mind.  Every time you send
> a binary attachment, it has to be ASCII-armoured which increases it's
> size, often 2-3 times the original binary file size.

Almost never.  Base64 encoding uses a 7-bit ASCII character to represent 6
bits from the attachment, so it has a 33% overhead.  Note that the resulting
encoding is trivially compressible to nearly the original size; any
intermediate storage or transfer mechanism that uses compression (such as
TLS) will use almost no more space than the original file.

> HTTP and FTP are good ways to send files.  Put your file on such aq
> server and include a simple link to it in your e-mail.

I disagree.  Why?

  1) When you send email, *you* control when you're going to use your
     bandwidth.  If you sent out 10,000 links to a file on your server, and
     everyone decides to follow the link at the same time, then you might be
     in trouble.

  2) Modern MTAs merge transmitions to given domains.  If 2500 out of your
     10,000 emails are to aol.com users, then your mailserver could
     reasonably send exactly one mail to aol.com, and specify that it should
     go to all 2500 recipients.  That mitigates the bandwidth factor
     somewhat.

  3) People are lazy.  If you're trying to send, say, a monthly newsletter
     in PDF form to your customers, then the odds of them opening an
     attachment are *much* higher than them clicking on a link to open a
     browser and visit a webpage.  Plus, if the file is on their mailserver
     or their client, then they don't have to re-download it from you if
     they want to view it again a month from now.

  4) When you sent email, you're making an *outbound* connection.  When you
     host the files locally, you have to configure your firewall and
     services to allow access to the files in question but nothing more.
     From a security point of view, I'd much rather establish connections
     than accept them.

FTP and HTTP obviously have their uses, but it's not fair to use the reason
that "email was not designed" to recommend against doing exactly that.  It
has its place.
=2D --=20
Kirk Strauser
The Strauser Group
Open. Solutions. Simple.
http://www.strausergroup.com/
=2D----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFALuz+5sRg+Y0CpvERAowvAJ9nDaPI0SYP4WrxcQTfApR17KJ99QCfed87
jjR+GguN+F2O6ASi60bSZMI=3D
=3Dv42d
=2D----END PGP SIGNATURE-----


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

Date: Sat, 14 Feb 2004 17:21:05 -0800
From: "Brad Walton" <sammie-nospam@greatergreen.com>
Subject: Re: Sending HASH over TCP
Message-Id: <T-mdnb1O5qrhVLPdRVn-tA@comcast.com>

> I recommended using nstore_fd and fd_retrieve for several reasons:
> ...

Thanks Ben, I will test this out. Appreciate the help!

Brad




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

Date: 14 Feb 2004 17:53:37 -0800
From: Prab_kar@hotmail.com (Prabh)
Subject: Understanding 'scope'
Message-Id: <e7774537.0402141753.378a40a@posting.google.com>

Hello all,
Is it possible to access a variable with local scope in a subroutine
from outside of the subroutine?

e.g.,

#usr/local/bin/perl

use strict ;
use warnings ;

sub testScope 
{
   my $var = 10 ;
}

&testScope ;
print "$var\n" ;

==========================================

It doesnt compile with 'use strict', after removing strict, I get
blank from the print.

Is it possible to retrieve $var in some 'testScope::$var' fashion?
I realize I could add "return $var;" to the subroutine and access it
from the out, but was just curious if its possible to access
variables.

If I dont use "my" or "strict" I can.  
If its a frowned-upon practice, then why does Perl allow it in the
first place?

Thanks for your time,
Prab


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

Date: Sun, 15 Feb 2004 02:17:55 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Understanding 'scope'
Message-Id: <c0mksj$d6k$1@wisteria.csv.warwick.ac.uk>


Prab_kar@hotmail.com (Prabh) wrote:
> Is it possible to access a variable with local scope in a subroutine
> from outside of the subroutine?

No. That is the meaning of 'scope'.

> #usr/local/bin/perl
> 
> use strict ;
> use warnings ;
> 
> sub testScope 
> {
>    my $var = 10 ;
> }
> 
> &testScope ;

Don't call subs with &:

    testScope;
or
    testScope();

> print "$var\n" ;
> 
> ==========================================
> 
> It doesnt compile with 'use strict', after removing strict, I get
> blank from the print.

What is happening there is that you are accessing the global variable
$main::var, which currently is set to undef. Try

    no strict 'vars';

    $main::var = "hello";

    sub test_scope {
        my $var = "world";
    }

    print "$var\n";

> Is it possible to retrieve $var in some 'testScope::$var' fashion?
> I realize I could add "return $var;" to the subroutine and access it
> from the out, but was just curious if its possible to access
> variables.

Nope. That's the point: as you can't get at $var from outside
testScope, nothing weird can happen to its value elsewhere in the
program without you realising.

See http://perl.plover.com/FAQs/Namespaces.html.

Ben

-- 
   If you put all the prophets,   |   You'd have so much more reason
   Mystics and saints             |   Than ever was born
   In one room together,          |   Out of all of the conflicts of time.
ben@morrow.me.uk |----------------+---------------| The Levellers, 'Believers'


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

Date: Sun, 15 Feb 2004 03:09:22 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: Understanding 'scope'
Message-Id: <Xns948FE16328233dkwwashere@216.168.3.30>

Prab_kar@hotmail.com (Prabh) wrote:

> Is it possible to access a variable with local scope in a subroutine
> from outside of the subroutine?

Ben Morrow already answered this, so I'll pick up a crumb he left behind...

> Is it possible to retrieve $var in some 'testScope::$var' fashion?
> I realize I could add "return $var;" to the subroutine and access it
> from the out, but was just curious if its possible to access
> variables.
> 
> If I dont use "my" or "strict" I can.  
> If its a frowned-upon practice, then why does Perl allow it in the
> first place?

Because IMHO Larry and the other authors of Perl don't like arbitrary 
restrictions on what a programmer can do. Package variables can be useful 
in one-liners: programs typed in at the command line, used once and never 
saved.  Or a programmer might choose to have global variables just to make 
life easier in some way. Or whatever.

Somewhat more facetiously: Perl gives you the freedom to program however 
you like. It's up to you to choose between Good and Evil. :-)

-- 
David Wall


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

Date: Sat, 14 Feb 2004 21:33:41 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Understanding 'scope'
Message-Id: <slrnc2tq4l.45f.tadmc@magna.augustmail.com>

Prabh <Prab_kar@hotmail.com> wrote:

> Is it possible to access a variable with local scope in a subroutine
> from outside of the subroutine?


No. (and that is a Good Thing)


> #usr/local/bin/perl
  ^^

    Do not re-type Perl code
        Use copy/paste or your editor's "import" function rather than
        attempting to type in your code. If you make a typo you will get
        followups about your typos instead of about the question you are
        trying to get answered.


Have you seen the Posting Guidelines that are posted here frequently?


> use strict ;
> use warnings ;
> 
> sub testScope 
> {
>    my $var = 10 ;
> }
> 
> &testScope ;
> print "$var\n" ;
> 
>==========================================
> 
> It doesnt compile with 'use strict', after removing strict, I get
> blank from the print.


But you also get a warning too, right?

What did it say?


> Is it possible to retrieve $var in some 'testScope::$var' fashion?


Yes and no.  :-)


Yes, if you make $var a package variable rather than a lexical variable:

   our $var = 10;

(but that might be a Bad Thing, so I hope you don't _want_ that)

(and the "fashion" would have the dollar sign first:  $main::var )


No, because that is what you asked for when you decided to my()
the variable. (and that is a Good Thing)


> If its a frowned-upon practice, then why does Perl allow it in the
> first place?


"frowned-upon" does not mean "absolutely never", so Perl allows it
to account for those rare cases.


Global variables are bad (in general). By turning on "use strict",
perl was able to tell you that you were using a global variable.

That is a Very Very Good Thing. It caught your mistake for you.


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


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

Date: Sun, 15 Feb 2004 04:13:18 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Understanding 'scope'
Message-Id: <x7fzdd9e01.fsf@mail.sysarch.com>

>>>>> "DKW" == David K Wall <dwall@fastmail.fm> writes:

  DKW> Because IMHO Larry and the other authors of Perl don't like
  DKW> arbitrary restrictions on what a programmer can do. Package
  DKW> variables can be useful in one-liners: programs typed in at the
  DKW> command line, used once and never saved.  Or a programmer might
  DKW> choose to have global variables just to make life easier in some
  DKW> way. Or whatever.

package globals have many important uses. they are just not the ones
newbies typically use them for. stuff that needs to be exported is one
common use. or setting flags inside a module that doesn't want to have
class methods for that.

  DKW> Somewhat more facetiously: Perl gives you the freedom to program however 
  DKW> you like. It's up to you to choose between Good and Evil. :-)

perl supporting package globals is needed. using them is not always
needed. is that helpful?

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: Sat, 14 Feb 2004 23:12:32 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: XML::Simple array size
Message-Id: <c0ma10$9qv$1@wisteria.csv.warwick.ac.uk>


Mark J Fenbers <Mark.Fenbers@noaa.gov> wrote:
> -=-=-=-=-=-
> [Attachment type=text/x-vcard, name=Mark.Fenbers.vcf]
> -=-=-=-=-=-

[DON'T post vcards here]
[please wrap your articles at 72 columns or so, to leave room for
 quoting]

>     my $main = XMLin("myfile.xml");
>     print $main->{level1}->{level2}->[3]->{content};
> 
> But I really want to know how many level2 records there are.  I
> tried variations of this:
> 
>     print scalar( $main->{level1}->{level2} );
> 
> but that gives me "ARRAY(0x80a2cb4)".  Can someone please help me
> formulate the proper Perl hash array syntax to determine the number
> of elements of the level2 array??

You're nearly there... apply Use Rule 1 from perlreftut:

write the statement with an ordinary array: 
    print scalar @array;
replace the *name* of the array with {}:
    print scalar @{ };
put an expression returning an arrayref in the braces:
    print scalar @{ $main->{level1}->{level2} };

Ben

-- 
  The cosmos, at best, is like a rubbish heap scattered at random.
                                                         - Heraclitus
  ben@morrow.me.uk


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

Date: Sat, 14 Feb 2004 21:10:24 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: XML::Simple array size
Message-Id: <slrnc2top0.45f.tadmc@magna.augustmail.com>

Mark J Fenbers <Mark.Fenbers@noaa.gov> wrote:

> This is a multi-part message in MIME format.


Why is this is a multi-part message in MIME format?

Usenet is a plain text medium.

Using MIME means that less people will see your posts.


>     print scalar( $main->{level1}->{level2} );
> 
> Can someone please help me formulate the
> proper Perl hash array syntax to determine the number of elements 
> of the level2
> array??


   print scalar( @{$main->{level1}->{level2}} );


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


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

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.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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


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