[30958] in Perl-Users-Digest
Perl-Users Digest, Issue: 2203 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Feb 11 14:09:53 2009
Date: Wed, 11 Feb 2009 11:09:17 -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 Wed, 11 Feb 2009 Volume: 11 Number: 2203
Today's topics:
Re: A good data structure to store INI files. <perl@marc-s.de>
Re: A good data structure to store INI files. <ben@morrow.me.uk>
Re: A good data structure to store INI files. <perl@marc-s.de>
Re: A good data structure to store INI files. <baxter.brad@gmail.com>
Re: A good data structure to store INI files. <perl@marc-s.de>
Re: A good data structure to store INI files. <ben@morrow.me.uk>
Re: A good data structure to store INI files. <perl@marc-s.de>
Adding path to @INC <guru.naveen@gmail.com>
Re: Adding path to @INC <peter@makholm.net>
Re: Adding path to @INC <perl@marc-s.de>
Re: Adding path to @INC <ben@morrow.me.uk>
Re: Adding path to @INC <tadmc@seesig.invalid>
Re: Adding path to @INC <devnull4711@web.de>
Re: Adding path to @INC <szrRE@szromanMO.comVE>
Re: Adding path to @INC <perl@marc-s.de>
Re: Adding path to @INC <jurgenex@hotmail.com>
Re: Adding path to @INC <perl@marc-s.de>
Re: Can I write CPAN Modules for a living? <brian.d.foy@gmail.com>
Re: Can I write CPAN Modules for a living? <cartercc@gmail.com>
Re: perl script open/save for voiceMessage problems <occitan@-remove-esperanto.org>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 11 Feb 2009 15:36:54 +0100
From: Marc Lucksch <perl@marc-s.de>
Subject: Re: A good data structure to store INI files.
Message-Id: <gmunpu$2q0b$1@ariadne.rz.tu-clausthal.de>
Ben Morrow schrieb:
> Quoth Marc Lucksch <perl@marc-s.de>:
>> But I had kind of an idea for a solution last night: By using
>> Contextual::Return in a FETCH of a tied object, I can do what I want
>> with the data. (Well actually it has grown to three tied objects) But it
>> works like this:
>
> You may want to look into objects with @{} and %{} overloading, rather
> than trying to make tie do something it doesn't want to. Having a tied
> variable with context-dependant return values is likely to be very
> confusing.
>
> It seems like you want $ini->{Ship} to return something that can pretend
> to be either a single value or an array of values. This is what @{}
> overloading is for. Of course, the @{} overload will itself probably
> need to return a tied array, but that's simple enough.
Thanks for it, but I already do it this way (I don't see any other)
My Data::INI looks like this:
package Data::INI;
require Data::INI::Entry;
require Data::INI::Value;
use Scalar::Util;
use Contextual::Return;
use Carp qw/confess cluck croak carp/;
use strict;
use warnings;
our $VERSION = 0.1;
use overload '%{}'=>\&gethash,'@{}'=>\&getarray,'""'=>\&serialize;
sub new {
my $class=shift;
my $self={hash=>{},array=>[]};
bless \$self,$class;
}
sub gethash {
my %h;
my $self = shift;
tie %h, ref $self, $self;
\%h;
}
sub getarray {
my @a;
my $self = shift;
tie @a, ref $self, $self;
\@a;
}
sub TIEARRAY {
my $class = shift;
my $self=shift;
$self=[$self,0];
bless \$self, $class;
}
sub TIEHASH {
my $class = shift;
my $self=shift;
$self=[$self,1];
bless \$self, $class;
}
[....]
sub FETCH {
my $self=shift;
my $obj=${${$self}->[0]};
if (${$self}->[1]) {
my $key=shift;
my $k=lc $key;
unless ($obj->{hash}->{$k}) {
my @a;
tie @a,"Data::INI::Entry",${$self}->[0],$k;
$obj->{hash}->{$k}=\@a;
return $obj->{hash}->{$k};
}
if (@{$obj->{hash}->{$k}} > 1) {
return $obj->{hash}->{$k};
}
if (@{$obj->{hash}->{$k}}) {
return (
ARRAYREF { $obj->{hash}->{$k} }
DEFAULT { $obj->{hash}->{$k}->[0] }
);
}
return (
ARRAYREF { $obj->{hash}->{$k} }
DEFAULT { undef }
);
}
else {
my $index=shift;
my $rindex=$index;
$index*=2;
$index++;
return undef unless $obj->{array}->[$index];
return ${$obj->{array}->[$index]};
}
}
[...]
Well it's getting there.
------------------------------
Date: Wed, 11 Feb 2009 15:07:39 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: A good data structure to store INI files.
Message-Id: <r66b66-8g2.ln1@osiris.mauzo.dyndns.org>
Quoth Marc Lucksch <perl@marc-s.de>:
> Ben Morrow schrieb:
> > Quoth Marc Lucksch <perl@marc-s.de>:
> >> But I had kind of an idea for a solution last night: By using
> >> Contextual::Return in a FETCH of a tied object, I can do what I want
> >> with the data. (Well actually it has grown to three tied objects) But it
> >> works like this:
> >
> > You may want to look into objects with @{} and %{} overloading, rather
> > than trying to make tie do something it doesn't want to. Having a tied
> > variable with context-dependant return values is likely to be very
> > confusing.
> >
> > It seems like you want $ini->{Ship} to return something that can pretend
> > to be either a single value or an array of values. This is what @{}
> > overloading is for. Of course, the @{} overload will itself probably
> > need to return a tied array, but that's simple enough.
>
> Thanks for it, but I already do it this way (I don't see any other)
OK. It seems you understand how this works well enough. What I meant
though was that here...
> sub FETCH {
> my $self=shift;
> my $obj=${${$self}->[0]};
> if (${$self}->[1]) {
> my $key=shift;
> my $k=lc $key;
> unless ($obj->{hash}->{$k}) {
> my @a;
> tie @a,"Data::INI::Entry",${$self}->[0],$k;
> $obj->{hash}->{$k}=\@a;
> return $obj->{hash}->{$k};
> }
> if (@{$obj->{hash}->{$k}} > 1) {
> return $obj->{hash}->{$k};
> }
> if (@{$obj->{hash}->{$k}}) {
> return (
> ARRAYREF { $obj->{hash}->{$k} }
> DEFAULT { $obj->{hash}->{$k}->[0] }
> );
...you return your own object with @{} and %{} (or presumably "" if it's
a leaf) overloading. While it's true that C::R does that for you, and
provides a rather clean syntax, the fact the module starts with
| package Contextual::Return;
|
| # Fake out CORE::GLOBAL::caller and Carp very early...
| BEGIN {
| no warnings 'redefine';
|
| *CORE::GLOBAL::caller = sub {
| my ($uplevels) = shift || 0;
| return CORE::caller($uplevels + 2 + $Contextual::Return::uplevel)
| if $Contextual::Return::uplevel;
| return CORE::caller($uplevels + 1);
| };
|
| use Carp;
| my $real_carp = *Carp::carp{CODE};
| my $real_croak = *Carp::croak{CODE};
|
| *Carp::carp = sub {
| goto &{$real_carp} if !$Contextual::Return::uplevel;
| warn _in_context(@_);
| };
and goes on to do other equally evil things would make me very wary of
it. It's not hard to create the object yourself.
Ben
------------------------------
Date: Wed, 11 Feb 2009 16:49:11 +0100
From: Marc Lucksch <perl@marc-s.de>
Subject: Re: A good data structure to store INI files.
Message-Id: <gmus1f$2ss8$1@ariadne.rz.tu-clausthal.de>
Ben Morrow schrieb:
> OK. It seems you understand how this works well enough. What I meant
> though was that here...
> ...you return your own object with @{} and %{} (or presumably "" if it's
> a leaf) overloading. While it's true that C::R does that for you, and
> provides a rather clean syntax, the fact the module starts with
>
> |[Evil stuff]
>
> and goes on to do other equally evil things would make me very wary of
> it. It's not hard to create the object yourself.
I thought of that first, but it is not so easy, as far as I understood
man overload right, I can't do that so easily.
Or maybe I'm reading it wrong:
|man overload (bottom of page):
| Relation between overloading and tie()ing is broken. Overloading is
| triggered or not basing on the previous class of tie()d value.
|
| This happens because the presence of overloading is checked too early,
| before any tie()d access is attempted. If the FETCH()ed class of the
| tie()d value does not change, a simple workaround is to access the
| value immediately after tie()ing, so that after this call the previous
| class coincides with the current one.
Marc "Maluku" Lucksch
------------------------------
Date: Wed, 11 Feb 2009 09:04:20 -0800 (PST)
From: Brad Baxter <baxter.brad@gmail.com>
Subject: Re: A good data structure to store INI files.
Message-Id: <68335a26-16b1-4a8d-8b56-3ce19c22858b@b16g2000yqb.googlegroups.com>
On Feb 11, 9:36 am, Marc Lucksch <p...@marc-s.de> wrote:
> Thanks for it, but I already do it this way (I don't see any other)
>
> My Data::INI looks like this:
Whether or not this will serve your needs, I think one fairly
straight-forward data structure that can faithfully represent
typical ini file data is pairs of pairs. A "pairs" structure
is an array of single-key hashes as defined in
http://yaml.org/type/pairs.html
In your case the typical ini data is enhanced by assuming that
comma separated values should be loaded as arrays.
The code below demonstrates what I mean.
Brad
#!/usr/local/bin/perl
use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 1;
# http://search.cpan.org/dist/Data-Pairs/
use Data::Pairs;
my $ini = Data::Pairs->new();
my $current;
while( <DATA> ) {
chomp;
if( /^\[(\w+)]/ ) {
my $section = $1;
$current = Data::Pairs->new();
$ini->add( $section => $current );
}
elsif( /^(\w+)\s*=\s*(.*)/ ) {
my( $key, $val ) = ( $1, $2 );
$val = [ split /\s*,\s*/, $val ] if $val =~ /,/;
$current->add( $key => $val );
}
else { # comments, blank lines, etc.
my $this = $current? $current: $ini;
$this->add( "" => $_ );
}
}
# view internal structure
print Dumper $ini;
# rewrite the ini file data
for( $ini->get_array() ) {
my( $section, $pairs ) = each %$_;
if( $section ) {
print "[$section]\n";
for( $pairs->get_array() ) {
my( $key, $val ) = each %$_;
$val = join( ', ', @$val ) if ref $val eq 'ARRAY';
print $key? "$key = $val\n": "$val\n";
}
}
else {
print "$pairs\n"; # comments/blank lines at the top
}
}
__DATA__
[Ship]
ids_name = 237033
ids_info = 66567
ids_info1 = 66567
ship_class = 1
nickname = li_elite
LODranges = 0, 75, 150, 1300
fuse = intermed_damage_smallship01, 0.000000, 400
fuse = intermed_damage_smallship02, 0.000000, 200
;#Well its a lot more in one section, but this proves the point.
[Ship]
ids_name = 237033
ids_info = 66567
ship_class = 1
nickname = li_elite2
LODranges = 0, 175, 150, 1300
fuse = intermed_damage_smallship01, 0.000000, 400
fuse = intermed_damage_smallship02, 0.000000, 200
fuse = intermed_damage_smallship03, 0.000000, 133
;.... (about 250kb of this)
------------------------------
Date: Wed, 11 Feb 2009 18:49:05 +0100
From: Marc Lucksch <perl@marc-s.de>
Subject: Re: A good data structure to store INI files.
Message-Id: <gmv329$31ib$1@ariadne.rz.tu-clausthal.de>
Brad Baxter schrieb:
> On Feb 11, 9:36 am, Marc Lucksch <p...@marc-s.de> wrote:
>> Thanks for it, but I already do it this way (I don't see any other)
>>
>> My Data::INI looks like this:
>
> Whether or not this will serve your needs, I think one fairly
> straight-forward data structure that can faithfully represent
> typical ini file data is pairs of pairs. A "pairs" structure
> is an array of single-key hashes as defined in
> http://yaml.org/type/pairs.html
I didn't know about that one, this is an completely object based model.
(Or function based). Had I known about this one beforehand, I probably
would have just used it.
But what I want to have now, is more a perlish way to represent this
kind of data. While there can be multiple keys in one section, it is
quite rare. (The only once I know from the top of my head are fuse= and
surface_hit_effects=). There are also .ini files with sections out there
which have unique names. The current access I want is more perlish and easy.
How would you for example set the second fuse of the third ship
(section?) An operation likely to occour...
my @ships=$ini->get_values( "Ships" );
my $third=$ships[2];
my @fuses=$third->get_pos("fuse");
$third->set_pos("fuse",["fusename",0,10],$fuses[1]);
OR
($ini->get_pos( "Ship"
))[2]->set_pos("fuse",["fusename",0,10],(($ini->get_pos( "Ship"
))[2]->get_pos("fuse"))[1]
I want it more like:
$ini->{Ship}->[2]->{"fuse"}->[1]=["fusename",0,10];
And for the nickname of the only pilot:
$ini->{Pilot}->{"nickname"}->[0]="li_pilot_elite"; #->[0] is needed
# otherwise it would be ambigious, add another nickname or replace the
# first/all.
$ini->{Pilot}->[0]->{"nickname"}->[0]->[0]="li_pilot_elite";
This makes it easier to use and that should be (IMHO) a big focus in
development, even if I have to rack my brain over this and/or annoy you
guys with posts about it. (Those actually helped me a lot, thanks to all
again)
__POD__
Short term and style definition: (ini-file, Value can be escaped)
[Section]
Key = Value [, Value]; Comment
[;SkippedSection]
> In your case the typical ini data is enhanced by assuming that
> comma separated values should be loaded as arrays.
Yes, and thanks again for the nice module hint, maybe you can add a
solution to my critism (Setting a value for specific key) to your nice
module
Marc "Maluku" Lucksch
------------------------------
Date: Wed, 11 Feb 2009 18:17:01 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: A good data structure to store INI files.
Message-Id: <t9hb66-du3.ln1@osiris.mauzo.dyndns.org>
Quoth Marc Lucksch <perl@marc-s.de>:
> Ben Morrow schrieb:
> > OK. It seems you understand how this works well enough. What I meant
> > though was that here...
>
> > ...you return your own object with @{} and %{} (or presumably "" if it's
> > a leaf) overloading. While it's true that C::R does that for you, and
> > provides a rather clean syntax, the fact the module starts with
> >
> > |[Evil stuff]
> >
> > and goes on to do other equally evil things would make me very wary of
> > it. It's not hard to create the object yourself.
>
> I thought of that first, but it is not so easy, as far as I understood
> man overload right, I can't do that so easily.
Since that's how Return::Contextual does it, it must be possible :). If
I've understood what you're trying to do correctly, the appended code
should cover it. (The modules will need renaming, of course.)
> Or maybe I'm reading it wrong:
>
> |man overload (bottom of page):
> | Relation between overloading and tie()ing is broken. Overloading is
> | triggered or not basing on the previous class of tie()d value.
> |
> | This happens because the presence of overloading is checked too early,
> | before any tie()d access is attempted. If the FETCH()ed class of the
> | tie()d value does not change, a simple workaround is to access the
> | value immediately after tie()ing, so that after this call the previous
> | class coincides with the current one.
I can't trigger this. If I've understood the rather bad English
correctly, the last set of tests (changing a not-array into an array and
expecting overload to immediately work) should, but clearly not. If you
can give an example of this failure, I'd be interested to see it.
Ben
# (Apologies for the length...)
#!/usr/bin/perl
use warnings;
use strict;
use Carp;
{
package Hashlike::Tie;
use Tie::Hash;
use base qw/Tie::ExtraHash/;
sub TIEHASH { bless [$_[1]], $_[0] }
sub FETCH {
my ($s, $k) = @_;
my $v = $s->[0]{$k};
if (ref $v eq "ARRAY") {
return Arraylike::Ref->new($v);
}
# I am assuming here that hashes never directly contain hashes.
return $v;
}
sub STORE {
my ($s, $k, $v) = @_;
my $h = $s->[0];
if (ref $v eq "ARRAY") {
$h->{$k} = $v;
}
else {
if (ref $h->{$k} eq "ARRAY") {
$h->{$k}[0] = $v;
}
else {
$h->{$k} = [$v];
}
}
}
}
{
package Arraylike::Ref;
sub new {
ref $_[1] eq "ARRAY" or die "$_[1] is not an ARRAY ref";
# Use a scalar ref as the object so we don't have to mess about
# trying to avoid overloading.
bless \$_[1], $_[0];
}
use overload
q/@{}/ => sub {
tie my @a, "Arraylike::Tie", ${$_[0]};
return \@a;
},
q/""/ => sub { ${$_[0]}->[0] },
q/%{}/ => sub {
my $v = ${$_[0]}->[0];
ref $v eq "HASH" or Carp::croak("$v is not a HASH ref");
tie my %h, "Hashlike::Tie", $v;
return \%h;
};
}
{
# For some reason there isn't a standard Tie::ExtraArray.
#
package Tie::ExtraArray;
use vars qw(@ISA);
@ISA = 'Tie::Array';
sub TIEARRAY { bless [[]], $_[0] }
sub FETCHSIZE { scalar @{$_[0][0]} }
sub STORESIZE { $#{$_[0][0]} = $_[1]-1 }
sub STORE { $_[0][0]->[$_[1]] = $_[2] }
sub FETCH { $_[0][0]->[$_[1]] }
sub CLEAR { @{$_[0][0]} = () }
sub POP { pop(@{$_[0][0]}) }
sub SHIFT { shift(@{$_[0][0]}) }
sub EXISTS { exists $_[0][0]->[$_[1]] }
sub DELETE { delete $_[0][0]->[$_[1]] }
# you need SPLICE, PUSH, UNSHIFT as well, of course
}
{
package Arraylike::Tie;
use Tie::Array;
use base qw/Tie::ExtraArray/;
sub TIEARRAY { bless [$_[1]], $_[0] }
sub FETCH {
my ($s, $k) = @_;
my $v = $s->[0][$k];
if (ref $v eq "HASH") {
tie my %h, "Hashlike::Tie", $v;
return \%h;
}
# I am assuming here that arrays never directly contain arrays.
return $v;
}
# Inherited STORE will do just fine.
}
use Test::More;
# You will need to normalise the data as you read it so that hashes
# never directly contain hashes. That is, instead of
#
# Pilot => { name => "bob" },
#
# you need
#
# Pilot => [ { name => "bob" } ],
#
# even if you only have one Pilot.
my %data = (
Ship => [
{
nickname => "li_elite",
fuse => [ "1.0000", "400" ],
},
{
nickname => "li_h4x0r",
fuse => [ "3.14159", "2.71828" ],
},
],
);
my $t;
BEGIN { $t += 6 }
tie my %ini, "Hashlike::Tie", \%data;
is $ini{Ship}{nickname}, "li_elite";
is $ini{Ship}[0]{nickname}, "li_elite";
is $ini{Ship}[1]{nickname}, "li_h4x0r";
is $ini{Ship}{fuse}, "1.0000";
is $ini{Ship}[1]{fuse}, "3.14159";
is $ini{Ship}[1]{fuse}[0], "3.14159";
BEGIN { $t += 3 }
$ini{Ship}{fuse} = "12345";
is $ini{Ship}{fuse}, "12345";
is $ini{Ship}{fuse}[0], "12345";
is $ini{Ship}{fuse}[1], "400";
BEGIN { $t += 3 }
$ini{Ship}{fuse} = ["333", "444"];
is $ini{Ship}{fuse}, "333";
is $ini{Ship}{fuse}[0], "333";
is $ini{Ship}{fuse}[1], "444";
BEGIN { $t += 3 }
$ini{Ship}{nickname} = ["foo", "bar"];
is $ini{Ship}{nickname}, "foo";
is $ini{Ship}{nickname}[0], "foo";
is $ini{Ship}{nickname}[1], "bar";
BEGIN { plan tests => $t }
------------------------------
Date: Wed, 11 Feb 2009 19:39:49 +0100
From: Marc Lucksch <perl@marc-s.de>
Subject: Re: A good data structure to store INI files.
Message-Id: <gmv61d$216$1@ariadne.rz.tu-clausthal.de>
Ben Morrow schrieb:
> Quoth Marc Lucksch <perl@marc-s.de>:
>> |man overload (bottom of page):
>> | Relation between overloading and tie()ing is broken. Overloading is
>> | triggered or not basing on the previous class of tie()d value.
>> |
>> | This happens because the presence of overloading is checked too early,
>> | before any tie()d access is attempted. If the FETCH()ed class of the
>> | tie()d value does not change, a simple workaround is to access the
>> | value immediately after tie()ing, so that after this call the previous
>> | class coincides with the current one.
>
> I can't trigger this. If I've understood the rather bad English
> correctly, the last set of tests (changing a not-array into an array and
> expecting overload to immediately work) should, but clearly not. If you
> can give an example of this failure, I'd be interested to see it.
>
> Ben
>
First up, thanks a lot for this, I think I will make it the way you
outlined it here. (But not today, tired). So I won't need C::R (which is
in fact kinda scary, but quite easy)
Second, I thought it was my fault for not understanding it, good thing
I'm not the only one (or is it a good thing?)
I'm currently on autovification
my $ini=Data::INI->new();
$ini{Ship}{nickname}="li_elite";
and this cruft:
push @{$ini{Ship}},$data;
$#{$ini}=1;
%{$ini}=();
$#{$ini->{Ship}}=10;
$ini->{Copy}=$ini->{Ship};
And so on. (above already works)
Marc "Maluku" Lucksch
'You shouldn't mistake spam for social contact'
------------------------------
Date: Wed, 11 Feb 2009 06:26:54 -0800 (PST)
From: guru <guru.naveen@gmail.com>
Subject: Adding path to @INC
Message-Id: <ea88bee1-44fa-4876-a31d-a0e1424d4943@z2g2000prg.googlegroups.com>
Can i use following statement to push path contained in $LINK_PATH to
@INC array.
$LINK_PATH = "/usr/raja/"
BEGIN {
push @INC,"$LINK_PATH";
}
when I see contents of @INC it is not adding.
I am trying to refer the module which is in other directory.
I will give path in command line. in the script I will get this path
and try to put in to @INC so that I can find those modules.
Can I use the above statement
BEGIN {
push @INC,'$LINK_PATH';
}
or how this can be done.
Thanks & Regards
Gururaja B O
------------------------------
Date: Wed, 11 Feb 2009 15:38:52 +0100
From: Peter Makholm <peter@makholm.net>
Subject: Re: Adding path to @INC
Message-Id: <87skmlavib.fsf@vps1.hacking.dk>
guru <guru.naveen@gmail.com> writes:
> $LINK_PATH = "/usr/raja/"
This line is outside the BEGIN block and is such executed at normal
run time.
> BEGIN {
> push @INC,"$LINK_PATH";
This line is inside the BEGIN block ans is as such executed at compile
time. At this time $LINK_PATH doesn't have any content. Put both lines
inside the BEGIN block or just do away with $LINK_PATH.
> }
//Makholm
------------------------------
Date: Wed, 11 Feb 2009 15:44:30 +0100
From: Marc Lucksch <perl@marc-s.de>
Subject: Re: Adding path to @INC
Message-Id: <gmuo86$2qgn$1@ariadne.rz.tu-clausthal.de>
guru schrieb:
> Can i use following statement to push path contained in $LINK_PATH to
> @INC array.
>
> $LINK_PATH = "/usr/raja/"
>
> BEGIN {
> push @INC,"$LINK_PATH";
> }
>
You made 2 mistakes:
1. You didn't use strict or warnings.
2. BEGIN gets run before the $LINK_PATH = "/usr/raja/", strict would
have told you that.
G:\Temp\INI>perl
$LINK_PATH = "/usr/raja/";
BEGIN {
push @INC,"$LINK_PATH";
}
print @INC;
__END__
C:/usr/site/libC:/usr/lib.
G:\Temp\INI>perl
BEGIN {
LINK_PATH = "/usr/raja/";
push @INC,"$LINK_PATH";
}
print @INC;
__END__
C:/usr/site/libC:/usr/lib./usr/raja/
Alwyas use strict. ALWAYS.
G:\Temp\INI>perl
use strict;
use warnings;
$LINK_PATH = "/usr/raja/";
BEGIN {
push @INC,"$LINK_PATH";
}
Global symbol "$LINK_PATH" requires explicit package name at - line 3.
Global symbol "$LINK_PATH" requires explicit package name at - line 5.
G:\Temp\INI>perl
use strict;
use warnings;
my $LINK_PATH = "/usr/raja/";
BEGIN {
push @INC,"$LINK_PATH";
}
Use of uninitialized value $LINK_PATH in string at - line 5.
By the way: Better use lib:
use lib qw[/usr/raja/];
OR: on commandline, use the -I switch.
G:\Temp\INI>perl -I /usr/raja
print @INC;
__END__
/usr/rajaC:/usr/site/libC:/usr/lib.
------------------------------
Date: Wed, 11 Feb 2009 14:50:49 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Adding path to @INC
Message-Id: <975b66-m92.ln1@osiris.mauzo.dyndns.org>
Quoth guru <guru.naveen@gmail.com>:
> Can i use following statement to push path contained in $LINK_PATH to
> @INC array.
>
> $LINK_PATH = "/usr/raja/"
>
> BEGIN {
> push @INC,"$LINK_PATH";
Don't quote variables when you don't need to. See perldoc -q quoting.
> }
>
> when I see contents of @INC it is not adding.
The problem is that you push onto @INC at BEGIN time, but the variable
is not set until normal runtime. To put that another way, because of the
BEGIN the push statement actually executes before the assignment, so
you are pushing undef onto @INC rather than the string. Either remove
the BEGIN, or, if you need it there, put the assignment inside as well.
> I will give path in command line. in the script I will get this path
> and try to put in to @INC so that I can find those modules.
If you are trying to set $LINK_PATH out of @ARGV, you will need to do
all of that inside a BEGIN block as well.
> Can I use the above statement
>
> BEGIN {
> push @INC,'$LINK_PATH';
This is not the same as you had before: did you realise that? The single
quotes will push the literal string '$LINK_PATH' onto @INC. I think you
may be confusing Perl with shell: unlike in shell, strings are never
processed for variable references later (unless you use ask for it with
eval), so the compex layers of single and double quotes you end up with
in shell aren't necessary.
Ben
------------------------------
Date: Wed, 11 Feb 2009 08:56:02 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Adding path to @INC
Message-Id: <slrngp5po2.mij.tadmc@tadmc30.sbcglobal.net>
guru <guru.naveen@gmail.com> wrote:
> Can i use following statement to push path contained in $LINK_PATH to
> @INC array.
No.
> $LINK_PATH = "/usr/raja/"
>
> BEGIN {
> push @INC,"$LINK_PATH";
> }
>
> when I see contents of @INC it is not adding.
Because the push() is executed *before* the assignment to $LINK_PATH.
See also:
perldoc -q vars
What's wrong with always quoting "$vars"?
> I am trying to refer the module which is in other directory.
>
> I will give path in command line. in the script I will get this path
> and try to put in to @INC so that I can find those modules.
You should check the Perl FAQ before posting to the Perl newsgroup.
perldoc -q INC
How do I add a directory to my include path (@INC) at runtime?
> or how this can be done.
use lib $ARGV[0];
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Wed, 11 Feb 2009 16:35:35 +0100
From: Frank Seitz <devnull4711@web.de>
Subject: Re: Adding path to @INC
Message-Id: <6vg9hvFju8ppU1@mid.individual.net>
guru wrote:
>
> I am trying to refer the module which is in other directory.
use lib '/usr/raja';
Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
------------------------------
Date: Wed, 11 Feb 2009 09:17:03 -0800
From: "szr" <szrRE@szromanMO.comVE>
Subject: Re: Adding path to @INC
Message-Id: <gmv16f010i6@news2.newsguy.com>
Marc Lucksch wrote:
> guru schrieb:
>> Can i use following statement to push path contained in $LINK_PATH to
>> @INC array.
>>
>> $LINK_PATH = "/usr/raja/"
>>
>> BEGIN {
>> push @INC,"$LINK_PATH";
>> }
>>
>
> You made 2 mistakes:
>
> 1. You didn't use strict or warnings.
You cannot say this conclusively, not without seeing his real code. It
appeared to me he was just giving small example snippets (which he may
just typed out from memory,) which I would agree may not have been the
best way to go about it.
> 2. BEGIN gets run before the $LINK_PATH = "/usr/raja/", strict would
> have told you that.
Strict tells no such thing:
$ perl -e 'use strict; use warnings; my $f = "/tmp";
> BEGIN { push @INC, $f }'
This returns nothing with both perl 5.10.0 and 5.8.8. If you put double
quotes around $f in the push statement, you get a warning about the use
of an uninitialized value in a string, but this has no baring on weather
or not strict was used. (And for that matter, putting double quotes
around a lone variable like that is generally bad practice.)
In fact the only warning I get at all is from his first snippet is about
the fact "my" was omitted from $LINK_PATH, which is coming from
'warnings', not 'strict.'
[...]
> By the way: Better use lib:
>
> use lib qw[/usr/raja/];
Indeed.
If it needs to be set dynamically (ie, from a variable), one can do
something like:
$ perl -e 'use strict; use warnings; my $lib;
> BEGIN { $lib = "/foo"; } use lib ($lib);
> print join "\n", @INC;'
/foo
/home/szr/perllib
/usr/lib/perl5/5.10.0/i686-linux-64int-ld
/usr/lib/perl5/5.10.0
/usr/lib/perl5/site_perl/5.10.0/i686-linux-64int-ld
/usr/lib/perl5/site_perl/5.10.0
.
I believe that there are some modules out there that do something like
this, although those are usually very special circumstances.
--
szr
------------------------------
Date: Wed, 11 Feb 2009 18:59:45 +0100
From: Marc Lucksch <perl@marc-s.de>
Subject: Re: Adding path to @INC
Message-Id: <gmv3m9$cj$1@ariadne.rz.tu-clausthal.de>
szr schrieb:
> Marc Lucksch wrote:
>> guru schrieb:
>>> Can i use following statement to push path contained in $LINK_PATH to
>>> @INC array.
>>>
>>> $LINK_PATH = "/usr/raja/"
>>>
>>> BEGIN {
>>> push @INC,"$LINK_PATH";
>>> }
>>>
>> You made 2 mistakes:
>>
>> 1. You didn't use strict or warnings.
>
> You cannot say this conclusively, not without seeing his real code. It
> appeared to me he was just giving small example snippets (which he may
> just typed out from memory,) which I would agree may not have been the
> best way to go about it.
Your right,
>> 2. BEGIN gets run before the $LINK_PATH = "/usr/raja/", strict would
>> have told you that.
>
> Strict tells no such thing:
>
> $ perl -e 'use strict; use warnings; my $f = "/tmp";
> > BEGIN { push @INC, $f }'
>
> This returns nothing with both perl 5.10.0 and 5.8.8. If you put double
> quotes around $f in the push statement, you get a warning about the use
> of an uninitialized value in a string, but this has no baring on weather
> or not strict was used. (And for that matter, putting double quotes
> around a lone variable like that is generally bad practice.)
>
> In fact the only warning I get at all is from his first snippet is about
> the fact "my" was omitted from $LINK_PATH, which is coming from
> 'warnings', not 'strict.'
And right again. I should keep my gob shut. :(
I still think the -I switch is the best way to do this without any code.
(If that lib in $ARGV[0] of course.
perl sthwithinsert.pl "/lib/raja/"
perl sth.pl -I "/lib/raja/"
However you would have to modify the callig code, or can you modify the
arguments to perl from the script? (Stranger things are possible)
You also have to think about the security implications, if this is a
CGI-script, then I would NEVER use any of this. -I is even worse, it
does unshift @INC, not push @INC, you could overwrite any module.
------------------------------
Date: Wed, 11 Feb 2009 10:07:44 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Adding path to @INC
Message-Id: <as46p41706510eofes0cat16mirs0ffqid@4ax.com>
Marc Lucksch <perl@marc-s.de> wrote:
>Your right,
What about his right? Did you mean his right side or his legal or moral
right for something?
jue
------------------------------
Date: Wed, 11 Feb 2009 19:42:26 +0100
From: Marc Lucksch <perl@marc-s.de>
Subject: Re: Adding path to @INC
Message-Id: <gmv669$216$2@ariadne.rz.tu-clausthal.de>
Jürgen Exner schrieb:
> Marc Lucksch <perl@marc-s.de> wrote:
>> Your right,
>
> What about his right? Did you mean his right side or his legal or moral
> right for something?
It's his right to be right on the right side of the right.
It should have been:
> You're right!
or
> You are right!
Sorry I'm both tired and not good at english (worst in my class in fact)
Marc "Maluku" Lucksch
------------------------------
Date: Wed, 11 Feb 2009 11:50:10 -0600
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: Can I write CPAN Modules for a living?
Message-Id: <110220091150106151%brian.d.foy@gmail.com>
In article <frb4p4h9tr4lq4u82bdu5mgrcpnccvk8ls@4ax.com>,
<sln@netherlands.com> wrote:
> I could learn anything an eployer needs, that I don't know in about 26
> minutes,
> thats the way it is u know. When you know HOW to find things out quickly and
> the
> absorption is instantaneous, because it fits in your broad substructure
> framework of
> knowledge.
>
> So, I ask this humble question: Can I make money writing CPAN Modules?
> If not, what good are CPAN Modules for anybody at all?
Yes, you can make a living writing CPAN modules. However, the better
way to make a living is to make friends and not be a jerk to everyone
you run across. The good people almost never have to job hunt because
their network of friends do it for them.
And effective communication skills, such as spelling, grammar, and
diction often impress employers too.
------------------------------
Date: Wed, 11 Feb 2009 10:57:43 -0800 (PST)
From: cartercc <cartercc@gmail.com>
Subject: Re: Can I write CPAN Modules for a living?
Message-Id: <12dbf2d8-6738-445d-88f6-eb15c30b4f97@m40g2000yqh.googlegroups.com>
Your first post on c.l.p.m. was in July, 2008. Replying to
RedGrittyBrick, a poster who has contributed far more than you, you
said,
"You got shit fo brains my friend.
sln"
Somehow, I can't quite work up a lot of sympathy for you, but I do
have a suggestion. If you can't find a job in the IT sector, try other
sectors, like maybe the fast food sector or the security guard sector.
I understand about getting hired. I've interviewed for IT jobs now for
a number of years (not really seeking a new job, just looking around,
because I've always had someone willing to pay me regularly). My take
is that it's very hard to be hired cold, that the largest number of IT
jobs go to those that already are a known quality to the employer.
Maybe this is why you are having a hard time ... you are already a
known quality to your pool of employers.
Despite my snide remark, I wish you the best, and if you feel that I
can do anything for you, please contact me privately.
CC
------------------------------
Date: Wed, 11 Feb 2009 10:18:21 -0800
From: "Daniel Pfeiffer" <occitan@-remove-esperanto.org>
Subject: Re: perl script open/save for voiceMessage problems
Message-Id: <6vgj3fFk3f9kU1@mid.individual.net>
Tad J McClellan wrote:
> paul.hopgood@deathnotify.com <paul.hopgood@deathnotify.com> wrote:
>
> > Any suggestions would be helpful.
>
>
> You should always enable strictures in Perl programs.
True, as two others had posted over 3 hours befor you said as well.
> > #!/usr/bin/perl -w
>
>
> use strict;
>
>
> > open (SAVE, "> $deathnotify.com/recordings");
>
>
> "use strict" would have found the bug in the line above if you
> had only asked it to...
I wouldn't call it a bug, I'd call it sloppy copy-pasting and/or
from-memory typing.
------------------------------
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 V11 Issue 2203
***************************************