[32900] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4178 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Mar 22 14:09:30 2014

Date: Sat, 22 Mar 2014 11:09:04 -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           Sat, 22 Mar 2014     Volume: 11 Number: 4178

Today's topics:
    Re: _ check <gamo@telecable.es>
    Re: _ check <rweikusat@mobileactivedefense.com>
        dynamically creating a hash from an array <cartercc@gmail.com>
    Re: dynamically creating a hash from an array <rweikusat@mobileactivedefense.com>
    Re: dynamically creating a hash from an array <jurgenex@hotmail.com>
    Re: dynamically creating a hash from an array <news@todbe.com>
    Re: dynamically creating a hash from an array <gravitalsun@hotmail.foo>
    Re: What is this module using? (Tim McDaniel)
    Re: What is this module using? <rweikusat@mobileactivedefense.com>
    Re: What is this module using? (Tim McDaniel)
    Re: What is this module using? <rweikusat@mobileactivedefense.com>
    Re: What is this module using? (Tim McDaniel)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 21 Mar 2014 23:14:08 +0100
From: gamo <gamo@telecable.es>
Subject: Re: _ check
Message-Id: <lgidjc$jd1$1@speranza.aioe.org>

El 20/03/14 21:43, George Mpouras escribió:
> first I check if a file exists ( -f $file ) and then doing several tests
> using the _ for being fast.
>
> I have serious problem because the _ checks e.g -z _ sometimes is giving
> wrong results. the code is multi thread . What is going wrong ?


The special filehandle '_' is different from using a normal variable.
So, if something is not working try a normal variable.

If that don't works, try something like this

             $can_perhaps_read = -r "file";      # use the mode bits
            {
                use filetest 'access';          # intuit harder
                $can_really_read = -r "file";
            }
            $can_perhaps_read = -r "file";      # use the mode bits again

Which is copyed from 'man filetest'

-- 
http://www.telecable.es/personales/gamo/


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

Date: Fri, 21 Mar 2014 22:27:40 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: _ check
Message-Id: <87k3bnkx0j.fsf@sable.mobileactivedefense.com>

gamo <gamo@telecable.es> writes:
> El 20/03/14 21:43, George Mpouras escribió:
>> first I check if a file exists ( -f $file ) and then doing several tests
>> using the _ for being fast.
>>
>> I have serious problem because the _ checks e.g -z _ sometimes is giving
>> wrong results. the code is multi thread . What is going wrong ?
>
>
> The special filehandle '_' is different from using a normal variable.
> So, if something is not working try a normal variable.

This depends on your definition of 'normal variable': perl keeps the
result of the last stat call in a 'normal variable' named PL_statbuf
(actually, this is a macro expanding to the 'normal variable' but that's
not really relevant here) and doing a filetest on _ examines the struct
stat available as/ via PL_statbuf. 

But this doesn't really matter: The state of 'filesystem objects' can
generally change at any time and this means the result of each stat call
is potentially outdated as soon as it becomes available. And the same is
true about checks done via access(2), as filetest does: No matter what
the call returned, it is potentially not correct anymore by the time the
result is available.


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

Date: Fri, 21 Mar 2014 09:34:47 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: dynamically creating a hash from an array
Message-Id: <15274bba-0d3a-4b71-9a65-67811ed9c21c@googlegroups.com>

To be honest, this may be a FAQ, but I don't seem to have the vocabulary to find it.

Suppose I have a set of variables, like this:
my $username = 'Joe';
my $password = 'somepassword';
my $role = 'user';
my $session = 'q1w2e3r4t5y6';
my $date = '2014-03-21';
my $time = '12345678';

Now, suppose I have a sub that prints the name and value of each variable, like this:
sub debug
{
  my $href = shift;
  print "-------------begin debugging data------------\n";
  foreach my $key (sort keys %{$href})
  {
    print "$key = $href->{$key}\n";
  }
  print "-------------end debugging data------------\n";
}

Further, suppose I have an array, like this:
my @debugarray = qw( username password role session date time );

Question: how can I turn the array into a hash so I can pass a reference to the has to the sub? Like this, perhaps: 
debug($href);

I suppose that my hash would look like this:
my $href = {
  username => $username,
  password => password,
  role => $role,
  session => $session,
  date => $date,
  time => $time,
};

As I say, this is probably frequently asked, but I may not know how to ask the question. Thanks CC.


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

Date: Fri, 21 Mar 2014 16:49:29 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: dynamically creating a hash from an array
Message-Id: <87wqfnlco6.fsf@sable.mobileactivedefense.com>

ccc31807 <cartercc@gmail.com> writes:
> To be honest, this may be a FAQ, but I don't seem to have the vocabulary to find it.
>
> Suppose I have a set of variables, like this:
> my $username = 'Joe';
> my $password = 'somepassword';
> my $role = 'user';
> my $session = 'q1w2e3r4t5y6';
> my $date = '2014-03-21';
> my $time = '12345678';

[...]

> Further, suppose I have an array, like this:
> my @debugarray = qw( username password role session date time );
>
> Question: how can I turn the array into a hash so I can pass a reference to the has to the sub? Like this, perhaps: 
> debug($href);
>
> I suppose that my hash would look like this:
> my $href = {
>   username => $username,
>   password => password,
>   role => $role,
>   session => $session,
>   date => $date,
>   time => $time,
> };

There's no way to access a 'my' variable 'by name' because at run time,
its "name" is an index into the lexical pad (term?) it belongs to. This
means the only way to achieve this is to generate a string containing
the Perl code creating the anonymous has and executing this code via
eval in a scope where the desired set of my variables exists. Example:

-------------
my $username = 'Joe';
my $password = 'somepassword';
my $role = 'user';
my $session = 'q1w2e3r4t5y6';
my $date = '2014-03-21';
my $time = '12345678';

my @debugarray = qw(username password role session date time);

sub make_hash_init
{
    my $code;

    $code = '{';
    $code .= sprintf('%s => $%s,', $_,$_) for @_;
    return $code.'}';
}

my $hdebug = eval(make_hash_init(@debugarray));

printf("%s\t%s\n", $_, $hdebug->{$_}) for keys(%$hdebug);
-------------


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

Date: Fri, 21 Mar 2014 10:08:48 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: dynamically creating a hash from an array
Message-Id: <7qroi91g9f352ofvq516q8u9l2ce6jup9l@4ax.com>

Rainer Weikusat <rweikusat@mobileactivedefense.com> wrote:
>ccc31807 <cartercc@gmail.com> writes:
>> To be honest, this may be a FAQ, but I don't seem to have the vocabulary to find it.
>>
>> Suppose I have a set of variables, like this:
>> my $username = 'Joe';
>> my $password = 'somepassword';
>> my $role = 'user';
>> my $session = 'q1w2e3r4t5y6';
>> my $date = '2014-03-21';
>> my $time = '12345678';
>
>[...]
>
>> Further, suppose I have an array, like this:
>> my @debugarray = qw( username password role session date time );
>>
>> Question: how can I turn the array into a hash so I can pass a reference to the has to the sub? Like this, perhaps: 
>> debug($href);


That is simple: use a better data structure.

%person = (username =>  'Joe',
	password => 'somepassword',
 	role => 'user',
	session => 'q1w2e3r4t5y6',
	date => '2014-03-21',
	time => '12345678'
)

Then passing a reference of the data set becomes trivial.

Anything else is at best a palliativ cover-up rather then a cure.

jue


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

Date: Fri, 21 Mar 2014 11:28:45 -0700
From: "$Bill" <news@todbe.com>
Subject: Re: dynamically creating a hash from an array
Message-Id: <lgi0co$7fc$1@dont-email.me>

On 3/21/2014 09:34, ccc31807 wrote:
>

Obviously this only gets the content of each var at the time the
hash is created - changing one of them won't change what's in the
hash:

use strict;
use warnings;

my $username = 'Joe';
my $password = 'somepassword';
my $role = 'user';
my $session = 'q1w2e3r4t5y6';
my $date = '2014-03-21';
my $time = '12345678';
my @debugarray = qw(username password role session date time);

my $href;
foreach (@debugarray) { eval "\$href->{$_} = \$$_"; }
debug ($href);
exit;

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub debug {
	my $href = shift;

print "-------------begin debugging data------------\n";
foreach my $key (sort keys %{$href}) {
	print "$key = $href->{$key}\n";
}
print "-------------end debugging data------------\n";

}

__END__



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

Date: Fri, 21 Mar 2014 21:00:00 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: dynamically creating a hash from an array
Message-Id: <lgi27k$d4m$1@news.ntua.gr>

# here you go

use strict;
use warnings;

my $username   = 'Joe';
my $password   = 'somepassword';
my $role       = 'user';
my $session    = 'q1w2e3r4t5y6';
my $date       = '2014-03-21';
my $time       = '12345678';
my @debugarray = qw( username password role session date time fakevar1 
fakevar2);
my %hash;



foreach my $var (@debugarray) {
$_ = eval "\$$var";
$hash{$var} = $_ unless $@
}


use Data::Dumper; print Dumper \%hash;


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

Date: Fri, 21 Mar 2014 19:22:14 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: What is this module using?
Message-Id: <lgi3h6$82o$1@reader1.panix.com>

Back about two years ago (Date: Wed, 18 Apr 2012 21:44:31 +0000 (UTC);
Message-ID: <jmncjv$ove$1@reader1.panix.com>), I wrote
> Looking at the docs, it appears that Devel::Modlist reports on *all*
> the modules used in an entire script.  Is there some way I can
> determine the modules used, directly or transitively, merely by the
> current module?

Ben Morrow suggested in <jmncjv$ove$1@reader1.panix.com>
> perl -d:Modlist -mMy::Module -e1 ?

I've gotten around to trying that.  The problem is that apparently I
had a different problem in mind than today's when I wrote

> determine the modules used, directly or transitively

With the method above, it's indeed transitive.  (Also, if I leave out
something from the INC path, it errors out at that "use" statement and
doesn't check anything after.  Our system does have a non-trivial
include list.)

For today's problem, I want only "directly", not "transitively".

My group is adding calls subs in new modules and sometimes forgetting
to add the "use" statements.  Each of our modules should "use"
everything it calls.  I'd like a somewhat automated way to check
statically for missing "use"s before we check in files.  So I do not
want transitive uses, and apparently we're doing a fair number of
transitive "use"s.

(I tried -d:Modlist as above with a library path that has only
Devel::Modlist, but the problem above arises: since I'm leaving out
many things from the INC path, it errors out at the first "use" of one
of our modules and doesn't report anything after.  And if I include our
path, as aforesaid I get a lot of transitive uses that I don't want.)

I'm thinking that I should just write a simple script to read a Perl
file looking for
- ^use foo::bar::baz
  save it as one of the uses (ignored any suffixed "nocritic", "no",
  or whatever)
- foo::bar::baz->
  confirm that foo::bar::baz is in the hash of uses
- foo::bar::baz::quux not followed by ->
  confirm that foo::bar::baz is in the hash of uses

We very rarely do "require".  We rarely do exports.  So the above
should work well enough 98% of the time.  I'm OK with false positives
in such odd cases, and false negatives (like calling an exported sub
where you forgot to "use" the module directly) should be rare and you
deserve the severe tire damage if you do it.

Comments?  Better ideas?

-- 
Tim McDaniel, tmcd@panix.com


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

Date: Fri, 21 Mar 2014 20:07:26 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: What is this module using?
Message-Id: <87ob0zl3i9.fsf@sable.mobileactivedefense.com>

tmcd@panix.com (Tim McDaniel) writes:

[...]

> My group is adding calls subs in new modules and sometimes forgetting
> to add the "use" statements.  Each of our modules should "use"
> everything it calls.  I'd like a somewhat automated way to check
> statically for missing "use"s before we check in files.

This may not be applicable to your problem but what about maintaining a
single list of use statements in a dedicated file?


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

Date: Fri, 21 Mar 2014 22:12:08 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: What is this module using?
Message-Id: <lgidfo$iia$1@reader1.panix.com>

In article <87ob0zl3i9.fsf@sable.mobileactivedefense.com>,
Rainer Weikusat  <rweikusat@mobileactivedefense.com> wrote:
>tmcd@panix.com (Tim McDaniel) writes:
>
>[...]
>
>> My group is adding calls subs in new modules and sometimes forgetting
>> to add the "use" statements.  Each of our modules should "use"
>> everything it calls.  I'd like a somewhat automated way to check
>> statically for missing "use"s before we check in files.
>
>This may not be applicable to your problem but what about maintaining a
>single list of use statements in a dedicated file?

Reading and parsing the entire codebase when starting the program
would take some time.

-- 
Tim McDaniel, tmcd@panix.com



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

Date: Fri, 21 Mar 2014 22:30:41 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: What is this module using?
Message-Id: <87fvmbkwvi.fsf@sable.mobileactivedefense.com>

tmcd@panix.com (Tim McDaniel) writes:
> Rainer Weikusat  <rweikusat@mobileactivedefense.com> wrote:
>>tmcd@panix.com (Tim McDaniel) writes:
>>
>>[...]
>>
>>> My group is adding calls subs in new modules and sometimes forgetting
>>> to add the "use" statements.  Each of our modules should "use"
>>> everything it calls.  I'd like a somewhat automated way to check
>>> statically for missing "use"s before we check in files.
>>
>>This may not be applicable to your problem but what about maintaining a
>>single list of use statements in a dedicated file?
>
> Reading and parsing the entire codebase when starting the program
> would take some time.

Insofar use statements are being used to include modules, the entire
codebase is 'read, parsed and compiled' prior to anything else
happening:

	It is exactly equivalent to

          BEGIN { require Module; Module->import( LIST ); }


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

Date: Fri, 21 Mar 2014 23:45:42 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: What is this module using?
Message-Id: <lgiiv6$acc$1@reader1.panix.com>

In article <87fvmbkwvi.fsf@sable.mobileactivedefense.com>,
Rainer Weikusat  <rweikusat@mobileactivedefense.com> wrote:
>tmcd@panix.com (Tim McDaniel) writes:
>> Rainer Weikusat  <rweikusat@mobileactivedefense.com> wrote:
>>>tmcd@panix.com (Tim McDaniel) writes:
>>>
>>>[...]
>>>
>>>> My group is adding calls subs in new modules and sometimes forgetting
>>>> to add the "use" statements.  Each of our modules should "use"
>>>> everything it calls.  I'd like a somewhat automated way to check
>>>> statically for missing "use"s before we check in files.
>>>
>>>This may not be applicable to your problem but what about maintaining a
>>>single list of use statements in a dedicated file?
>>
>> Reading and parsing the entire codebase when starting the program
>> would take some time.
>
>Insofar use statements are being used to include modules, the entire
>codebase is 'read, parsed and compiled' prior to anything else
>happening:

I've been trying to think about this in the general case.  The only
objection I can think of is that a program might not directly or
indirectly reach the entire codebase.  There might be several
programs, and different (perhaps overlapping, perhaps disjoint) parts
of the codebase might be used by different programs.  I think that
condition obtains at my ork-place.

The other objections I considered I don't think would actually be
problems:
- conflicting exports: they would have been happening before the "use
  unification"
- ordering problems with BEGIN blocks: that's such a disaster of a
  design that I don't think it needs to be considered.

Thank you for the idea and for prompting me to think under what
conditions it might or might not be useful.

-- 
Tim McDaniel, tmcd@panix.com




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

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:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 V11 Issue 4178
***************************************


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