[24402] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6590 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu May 20 21:10:42 2004

Date: Thu, 20 May 2004 18:10:10 -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           Thu, 20 May 2004     Volume: 10 Number: 6590

Today's topics:
    Re: SOLVED: How do I scope a variable if the variable n (David Filmer)
    Re: SOLVED: How do I scope a variable if the variable n (Malcolm Dew-Jones)
    Re: SOLVED: How do I scope a variable if the variable n <usenet@morrow.me.uk>
    Re: SOLVED: How do I scope a variable if the variable n <uri@stemsystems.com>
        Telnet module <abc@microsoft.com>
    Re: Telnet module <usenet@morrow.me.uk>
    Re: using modules <abc@microsoft.com>
    Re: validating IP addresses <usenet@morrow.me.uk>
    Re: Win32, FTP, line ends <lv@aol.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 20 May 2004 15:16:32 -0700
From: IneverReadAnythingSentToMe@hotmail.com (David Filmer)
Subject: Re: SOLVED: How do I scope a variable if the variable name contains a variable?
Message-Id: <e4c916dd.0405201416.39fc94eb@posting.google.com>

The fact that my last posting seems very clear to me but not to you is
troubling. Either I have it right but am expressing it poorly, or I
have it wrong. I believe I'm clearly expressing my understanding of
the subject, so I wonder if my understanding is all wrong. Please
allow me to briefly summarize my understanding, and prehaps you will
be kind enough to point out where I've gone astray.

>> show some code. your words are not conveying much useful info.

The code is at the top of the post. You didn't like it. You were
right. I don't have any better code to post, because, as I understand
it, what I was trying to do cannot be done (and thus cannot be coded),
and I need to do something else.

What I was trying to do was use a variable-as-a-variablename, and my
original question was how I could properly scope it, because

   my $foo = 'bar'; 
   my @{$foo};         #ie, @bar

doesn't work. I thought it was a syntax problem.

What I believe you've said (and what I've concluded) is that it cannot
be done at all (at least not without unleashing the demons of hell).
It is flat-out impossible to scope a variable (with a 'my' statement)
if the variable is not a hard reference. Is my understanding of this
correct?

You suggested using a hash INSTEAD of an array, correct? But I can't
do that, because I'm trying to pass this object to a module
(HTML::Template) which expects an array, ie:

   $template->param(THIS_LOOP => \@loop_data);

So I need to rewrite a section of my code to use a hard reference (ie,
@bar) instead of a symref (@{$foo} #where $foo eq 'bar'), and there's
no way around that (without unleashing the demons). Is this correct?


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

Date: 20 May 2004 16:56:29 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: SOLVED: How do I scope a variable if the variable name contains a variable?
Message-Id: <40ad45ad@news.victoria.tc.ca>

David Filmer (IneverReadAnythingSentToMe@hotmail.com) wrote:
: The fact that my last posting seems very clear to me but not to you is
: troubling. Either I have it right but am expressing it poorly, or I
: have it wrong. I believe I'm clearly expressing my understanding of
: the subject, so I wonder if my understanding is all wrong. Please
: allow me to briefly summarize my understanding, and prehaps you will
: be kind enough to point out where I've gone astray.

: >> show some code. your words are not conveying much useful info.

: The code is at the top of the post. You didn't like it. You were
: right. I don't have any better code to post, because, as I understand
: it, what I was trying to do cannot be done (and thus cannot be coded),
: and I need to do something else.

: What I was trying to do was use a variable-as-a-variablename, and my
: original question was how I could properly scope it, because

:    my $foo = 'bar'; 
:    my @{$foo};         #ie, @bar

: doesn't work. I thought it was a syntax problem.

: What I believe you've said (and what I've concluded) is that it cannot
: be done at all (at least not without unleashing the demons of hell).
: It is flat-out impossible to scope a variable (with a 'my' statement)
: if the variable is not a hard reference. Is my understanding of this
: correct?

Yes.  `my' allows the compiler, at compile time, to define a variable.  
Defining a variable at compile time means various things, including doing
such things as allocating memory and assigning a name to that memory
address.  When your code is running then various things have to happen,
including such things as accessing that location in memory.

If you want perl to be able to use a `my' variable then you have to give
perl all the information it needs about the variable at compile time.  
That includes the name, because that is how perl knows which address in
memory holds the data that implements that variable.

If you don't want to give all the data about the variable to the compiler
at compile time (i.e. its name)  then there are two things that can be
done (I doubt that either is what you need to do!)

One possibility is to use a global variable.  Perl doesn't need to know
the name at compile time because it looks up the name when the program is
running.  To do that, all perl has to know is where in memory the
information about names is stored, and it can calculate that at compile
time because that is simply one of the many internal data structures
created by the compiler itself.

This is very powerful, but is rarely the best way to solve a problem.  


A second possibility is to generate the code when the program runs.  That
way the name gets inserted into the code because the code is simply a
string, and perl doesn't compile it until you know the name.  This is even
more powerful, but again, is rarely the best way to solve a problem.

E.g.

	$variable_name = 'loop_data';
	$my_program_segment = 
	"	my \@$variable_name = (\"some value\");
		do_something_with(\@$variable_name);
	";
	eval $my_program_segment;
	die "$my_program_segment\n$@: $!" if $@;


I doubt that either of these are what you really need.  I suspect there is
some thing about programming perl that you are misunderstanding.  
Choosing which array to pass into a function should not be this difficult.


: You suggested using a hash INSTEAD of an array, correct? But I can't
: do that, because I'm trying to pass this object to a module
: (HTML::Template) which expects an array, ie:

:    $template->param(THIS_LOOP => \@loop_data);

that could be written as 

	my $ref_to_the_correct_array = 

	#
	# this is where I would put something to tell me which
	# array I wish to use.  I would show you the code but I
	# don't understand what you want well enough to be able to
	# do that.
	#

	# now use the array we just chose (sp?)

	$template->param(THIS_LOOP => $ref_to_the_correct_array);



--

(Paying) telecommute programming projects wanted.  Simply reply to this.



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

Date: Fri, 21 May 2004 00:35:56 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: SOLVED: How do I scope a variable if the variable name contains a variable?
Message-Id: <c8jitc$95n$1@wisteria.csv.warwick.ac.uk>


Quoth IneverReadAnythingSentToMe@hotmail.com (David Filmer):
> The fact that my last posting seems very clear to me but not to you is
> troubling. Either I have it right but am expressing it poorly, or I
> have it wrong. I believe I'm clearly expressing my understanding of
> the subject, so I wonder if my understanding is all wrong.

Others have explained most of where you're wrong. Basically:

1. Symrefs (using-a-variable-as-a-variable-name) only work with globals.
2. Symrefs don't work under use strict.
3. This is for a good reason: they are dangerous, and not usually
   necessary.

> You suggested using a hash INSTEAD of an array, correct? But I can't
> do that, because I'm trying to pass this object to a module
> (HTML::Template) which expects an array, ie:
> 
>    $template->param(THIS_LOOP => \@loop_data);
> 
> So I need to rewrite a section of my code to use a hard reference (ie,
> @bar)

This is not a hard reference... this is just an array variable. A symref
is something like this:

our @foo;          # must be global so we can use symrefs
my $bar = 'foo';
no strict 'refs';  # to allow symrefs
my @baz = @{$bar}; # @baz is now a copy of @foo

A hard ('proper') reference is something like this:

my @foo;           # can take hard refs to lexical vars
my $bar = \@foo;   # a ref, rather than just the name
my @baz = @{$bar}; # @baz is now a copy of @foo, again

> instead of a symref (@{$foo} #where $foo eq 'bar'), and there's
> no way around that (without unleashing the demons). Is this correct?

I don't think so... I presume you are trying to do something like this:

sub pass_this_param_to_template {
    # $param should be the name of an array
    my $param = shift;
    $template->param(THIS_LOOP => \@{$param});
}

my @foo = qw/.../;
my @bar = qw/.../;
pass_this_param_to_template 'foo';

, where the array to pass in is not known until runtime? Uri's
suggestion to use a hash is correct (it is almost always the correct
answer if you thought the answer was symrefs). You want to read perllol,
perlreftut and perldsc, and code something like this:

sub pass_this_param_to_template {
    # $param should be an arrayref
    my $param = shift;
    $template->param(THIS_LOOP => $param);
}

my %params = (
    foo => [qw/.../],
    bar => [qw/.../],
);
pass_this_param_to_template $params{foo};

Here the members of the hash are references to (anon) arrays, so you can
pass them straight into $template->param. Note that I haven't used a
hash *instead of* an array, I have used a hash *of* (references to)
arrays.

Ben

-- 
  Joy and Woe are woven fine,
  A Clothing for the Soul divine       William Blake
  Under every grief and pine          'Auguries of Innocence'
  Runs a joy with silken twine.                                ben@morrow.me.uk


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

Date: Thu, 20 May 2004 23:52:52 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: SOLVED: How do I scope a variable if the variable name contains a variable?
Message-Id: <x7brkimzaj.fsf@mail.sysarch.com>

>>>>> "DF" == David Filmer <IneverReadAnythingSentToMe@hotmail.com> writes:

  DF> The fact that my last posting seems very clear to me but not to you is
  DF> troubling. Either I have it right but am expressing it poorly, or I
  DF> have it wrong. I believe I'm clearly expressing my understanding of
  DF> the subject, so I wonder if my understanding is all wrong. Please
  DF> allow me to briefly summarize my understanding, and prehaps you will
  DF> be kind enough to point out where I've gone astray.

  >>> show some code. your words are not conveying much useful info.

  DF> The code is at the top of the post. You didn't like it. You were
  DF> right. I don't have any better code to post, because, as I understand
  DF> it, what I was trying to do cannot be done (and thus cannot be coded),
  DF> and I need to do something else.

symrefs CAN be done but they are nasty and evel and not needed for
general purpose data handling as you are doing.

  DF> What I was trying to do was use a variable-as-a-variablename, and my
  DF> original question was how I could properly scope it, because

  DF>    my $foo = 'bar'; 
  DF>    my @{$foo};         #ie, @bar

  DF> doesn't work. I thought it was a syntax problem.

use strict rightly disallows that.

  DF> What I believe you've said (and what I've concluded) is that it cannot
  DF> be done at all (at least not without unleashing the demons of hell).
  DF> It is flat-out impossible to scope a variable (with a 'my' statement)
  DF> if the variable is not a hard reference. Is my understanding of this
  DF> correct?

well, your wording is not perfect. my variables are not in the symbol
table so you can't get access to them via symrefs (even if they are allowed).

  DF> You suggested using a hash INSTEAD of an array, correct? But I can't
  DF> do that, because I'm trying to pass this object to a module
  DF> (HTML::Template) which expects an array, ie:

i meant use a hash instead of a symref (which is really using the symbol
table as a hash). 

  DF>    $template->param(THIS_LOOP => \@loop_data);

  DF> So I need to rewrite a section of my code to use a hard reference
  DF> (ie, @bar) instead of a symref (@{$foo} #where $foo eq 'bar'), and
  DF> there's no way around that (without unleashing the demons). Is
  DF> this correct?

i don't understand the problem. the param wants an array ref. the ref
could come from any array variable or be an anon array with []. this has
nothing to so with symrefs or hashes. you don't seem to understand perl
hard references. read perlreftut and perlref.

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: Thu, 20 May 2004 22:12:11 GMT
From: "Indigo5" <abc@microsoft.com>
Subject: Telnet module
Message-Id: <Hy19oA.CrC@news.boeing.com>

Is there a way to open several telnet objects simultaneously?  I have a
program that takes 10 hours to run and I have to run it for 200 datasets.  I
would like to use the Telnet module to do this.  However,  I can't figure
out how to return back to the main program after I launch the process. In
other words, I want to do some parallel processing with 36 different
machines.  Can this be done? In other words something like this currently
only works as serial proceessing:

   $matt1 = new Net::Telnet( Timeout => 36000,
                              Errmode => sub { main::defaultterm()},
                               Prompt => '/\$$/i'),

   $matt2 = new Net::Telnet( Timeout => 36000,
                             Errmode => sub { main::defaultterm()},
                             Prompt  => '/\$$/i'),

   $matt1->open($nodename{1});

   $matt2->open($nodename{2});


   $matt1->login("$uname\n","$upw\n");

   $matt2->login("$uname\n","$upw\n");


   $matt1->cmd("cd \/project\//$Runs{1}\/");

   $matt1->cmd("../main.pl . $Runs{1} -W");


   $matt2->cmd("cd \/project\/cafbeam\/SRM_RUNS\/$Runs{2}\/");

   $matt2->cmd("../main.pl . $Runs{2} -W");




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

Date: Fri, 21 May 2004 00:20:32 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Telnet module
Message-Id: <c8ji0g$8ev$1@wisteria.csv.warwick.ac.uk>


Quoth "Indigo5" <abc@microsoft.com>:
> Is there a way to open several telnet objects simultaneously?  I have a
> program that takes 10 hours to run and I have to run it for 200 datasets.  I
> would like to use the Telnet module to do this.  However,  I can't figure
> out how to return back to the main program after I launch the process. In
> other words, I want to do some parallel processing with 36 different
> machines.  Can this be done?

The obvious way is to fork 36 process / create 36 threads.

If you don't need to know when the jobs finish, you could simply put a &
on the end of each cmd, which will start the job in the background and
return immediately. You could also use nohup or the &! feature of zsh to
allow you to close the session immediately, if that is desirable.

Ben

-- 
perl -e'print map {/.(.)/s} sort unpack "a2"x26, pack "N"x13,
qw/1632265075 1651865445 1685354798 1696626283 1752131169 1769237618
1801808488 1830841936 1886550130 1914728293 1936225377 1969451372
2047502190/'                                                 # ben@morrow.me.uk


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

Date: Thu, 20 May 2004 22:05:43 GMT
From: "Indigo5" <abc@microsoft.com>
Subject: Re: using modules
Message-Id: <Hy19DK.CB0@news.boeing.com>

I have permissions wide open.  However I think the problem may be stemming
from the @INC array.  It appears that the default search path for typical
users is /opt/perl5.005_03/lib.  However, many moons ago when I was a Perl
newbie, I complained to my system admins about how they took Perl/Tk out of
the latest production install of Perl.  So, their solution was to point my
account at /opt/perl/lib/aix/5.00404.  Consequently, I think there is a
module that is being found in my search path and not in the other search
path.


"Ben Morrow" <usenet@morrow.me.uk> wrote in message
news:c8ir25$li9$4@wisteria.csv.warwick.ac.uk...
>
> Quoth ihatespam@hotmail.com:
> > Hi all,
> >
> > Quick question.  I've got a Perl program that runs fine from my account
at
> > work, but when someone else copies it and tries to run it from their
> > account, or when I telnet to another machine and try to run it, it
doesn't
> > work.  The error is something to the effect..."Can't find loadable
object
> > in Term::Readkey".
> >
> > At the top of my script I have
> >
> > use lib "/acct/mjo1234/perllib";
>
> Do other people have search access to this directory?
>
> Is this directory the same on the other machine you telnet to?
>
> Ben
>
> -- 
> It will be seen that the Erwhonians are a meek and long-suffering people,
> easily led by the nose, and quick to offer up common sense at the shrine
of
> logic, when a philosopher convinces them that their institutions are not
based
> on the strictest morality.  [Samuel Butler, paraphrased]
ben@morrow.me.uk




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

Date: Thu, 20 May 2004 21:40:35 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: validating IP addresses
Message-Id: <c8j8kj$1m6$1@wisteria.csv.warwick.ac.uk>


Quoth Ben Morrow <usenet@morrow.me.uk>:
> 
> Quoth Bob <bob.lockie.NOSPAM@mail.com>:
> > I need a function to accept an IP address (V4 or V6) and return if it is 
> > valid or not.
> 
> Regexp::Common has regexen for IPv4 addresses. I guess you could submit
> a patch to Abigail for IPv6.

Hmmm... having had a go at this, I've managed to come up with a
5527-char regex as the simplest solution I can find... is there
something I'm missing?

#!/usr/bin/perl -l

use Regexp::Common;

my $byte = qr/[[:xdigit:]]{1,2}/;

my ($v6full, $v4in6) = map {

    my $full = $_ - 1;
    my $var = $_ - 3;
    my @res = (
        qr/ (?:$byte:){$full} $byte /x,
        qr/ $byte? :: (?:$byte:){0,$var} $byte /x,
        qr/ $byte (?: :$byte){0,$var} :: $byte? /x,
    );

    for my $i (3 .. ($_-3)) {
        my $j = $_ - $i - 5;
        push @res, qr/$byte : $byte : (?:$byte:){0,$i} 
            : (?:$byte:){0,$j} $byte : $byte/x;
    }

    \@res;
    
} (16, 10);

push @$v4in6, ':';
my $ff = qr/ff/i;
my @v4in6 = map { 
    qr/$_ : $ff :? $ff : $RE{net}{IPv4}/x 
} @$v4in6;
$v4in6 = join '|', @v4in6;
$v6full = join '|', @$v6full;

my $IPv6 = qr/ $v4in6 | $v6full /x;

print $IPv6;
print length $IPv6;

while (<DATA>) {
    my ($addr, $valid) = split;
    my $test = ($addr =~ /^$IPv6$/ ? 'valid' : 'invalid');
    $test ne $valid and print "$addr : $test, should be $valid";
}

__END__
::1 valid
::FFFF:127.0.0.1 valid
::FF:FF:127.0.0.1 valid
::11:ff:127.0.0.1 invalid
0:0:0:0:0:0:0:0:0:0:0:ff:ff:127.0.0.1 invalid
0:0:0:0:0:0:0:0:0:0:Ff:fF:127.0.0.1 valid
1::ff:ff:255.255.255.255 invalid
::ff:FF:aa.0.0.0 invalid
:::1 invalid
1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16 valid
1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16: invalid
1:2:3:4:5:6:7:8:9:10:11:12:13:14:15::16 invalid
1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17 invalid
::0::1 invalid
1:1::1:1::1 invalid
:: invalid
a:b:c::d:e:f valid
a:b:c::d:e:f: invalid

Ben

-- 
"The Earth is degenerating these days. Bribery and corruption abound.
Children no longer mind their parents, every man wants to write a book,
and it is evident that the end of the world is fast approaching."
     -Assyrian stone tablet, c.2800 BC                         ben@morrow.me.uk


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

Date: Thu, 20 May 2004 18:28:54 -0500
From: l v <lv@aol.com>
Subject: Re: Win32, FTP, line ends
Message-Id: <40ad3f7f$1_2@corp.newsgroups.com>

Phil Hibbs wrote:
> l v <lv@aol.com> wrote in message news:<40ac0db4_2@corp.newsgroups.com>...
> 
>>Try ftping the file via the dos ftp command.  If the mainframe file 
>>still contains x'0D' at the end, then the mainframes ftp ebcdic to ascii 
>>translation tables need to be changed.
> 
> 
> Yes, Windows' built-in ftp command handles the line ends correctly.
> 
> Phil Hibbs.

Got some sample code?

I just tried ftping to our mainframe without problems, from what I could 
see.  I will try again when I am in the office.  I am running Net::FTP 
version 2.56 on perl 5.6.1 patch 631

Len


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 100,000 Newsgroups - 19 Different Servers! =-----


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

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


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