Re: redirect system command STDER

2011-07-20 Thread Christian Walde

On Tue, 19 Jul 2011 21:14:10 +0200, Tessio Fechine oiss...@gmail.com wrote:


Hello,
I have a subroutine that uses useradd to create accounts

--
@cmd = ('useradd', '-m', $account);
my $result = system @cmd;
--

but when useradd fails, I need to stop it from sending the error message to
STDER.
Is it possible with system?

Thanks!



For an easy and cross-platform solution you can use Capture::Tiny to capture 
both STDERR and STDOUT separately and then have your perl code decide what to 
do with each.

--
With regards,
Christian Walde

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: redirect system command STDER

2011-07-20 Thread Kevin Spencer
On Tue, Jul 19, 2011 at 12:14 PM, Tessio Fechine oiss...@gmail.com wrote:
 Hello,
 I have a subroutine that uses useradd to create accounts

 --
 @cmd = ('useradd', '-m', $account);
 my $result = system @cmd;
 --

 but when useradd fails, I need to stop it from sending the error message to
 STDER.
 Is it possible with system?

 Thanks!

Just as you would when using the shell, redirect STDERR somewhere
else: 2/dev/null.

Kevin.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: redirect system command STDER

2011-07-20 Thread Shlomi Fish
Hi Jim,

On Tue, 19 Jul 2011 16:22:14 -0700
Jim Gibson jimsgib...@gmail.com wrote:

 On 7/19/11 Tue  Jul 19, 2011  12:14 PM, Tessio Fechine oiss...@gmail.com
 scribbled:
 
  Hello,
  I have a subroutine that uses useradd to create accounts
  
  --
  @cmd = ('useradd', '-m', $account);
  my $result = system @cmd;
  --
  
  but when useradd fails, I need to stop it from sending the error message to
  STDER.
  Is it possible with system?
 
 You can use a shell process to discard STDERR messages (untested):
 
 my $cmd = useradd -m $account 2 /dev/null;
 my $result = system($cmd);
 

The problem with interpolating strings into shell commands like that is that
someone may put malicious code in it:

my $account = 'foo; rm -fr / ';

so be careful - see: http://shlomif-tech.livejournal.com/35301.html
(Code/Markup Injection and Its Prevention ).

Regards,

Shlomi Fish

 You could do the same by writing a shell script to redirect STDERR and call
 that from your Perl program.
 
 You can also use the IPC::Open3 module to capture STDERR. See 'perldoc
 IPC::Open3' for examples.
 
 
 
 



-- 
-
Shlomi Fish   http://www.shlomifish.org/
Apple Inc. is Evil - http://www.shlomifish.org/open-source/anti/apple/

An apple a day keeps the doctor away.
Two apples a day will keep two doctors away. 
— one of Shlomi Fish’s relatives

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Fw: redirect system command STDER

2011-07-20 Thread Shlomi Fish
Hi,

we keep receiving these messages when posting to beginners@perl.org. Can you
please unsubscribe lel...@claimspages.com from it?

Regards,

-- Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
My Aphorisms - http://www.shlomifish.org/humour.html

Chuck Norris can make the statement “This statement is false.” a true one.

Please reply to list if it's a mailing list post - http://shlom.in/reply .
---BeginMessage---
Valid fax destination information could not be found
in your mail message.  The message was discarded.

Examples of properly formatted messages:

  /name=frank/fax=3217...@faxgate.company.com
  Frank Smith /name=frank/fax=3217453/ faxg...@company.com

The original message information follows:

Received: from mx.npci.net ([10.0.1.60]) by mail.claimspages.com with
Microsoft SMTPSVC(5.0.2195.7381);
 Wed, 20 Jul 2011 05:43:48 -0400
Received: from localhost (unknown [127.0.0.1])
by mx.npci.net (Postfix) with ESMTP id 9D8A22E463E
for lel...@claimspages.com; Wed, 20 Jul 2011 09:43:48 + (UTC)
X-Virus-Scanned: amavisd-new at nationwide.net, cpdev20.com, claimspages.com
Received: from mx.npci.net ([127.0.0.1])
by localhost (del-mx1.nationwide.net [127.0.0.1]) (amavisd-new, port
10024)
with ESMTP id xErkade46R6G for lel...@claimspages.com;
Wed, 20 Jul 2011 05:43:43 -0400 (EDT)
Received: from x6.develooper.com (x6.develooper.com [207.171.7.86])
by mx.npci.net (Postfix) with ESMTP id 51DD42E4561
for lel...@claimspages.com; Wed, 20 Jul 2011 05:43:43 -0400 (EDT)
Received: from lists-nntp.develooper.com (localhost.localdomain [127.0.0.1])
by x6.develooper.com (Postfix) with SMTP id 1E86017ADD
for lel...@claimspages.com; Wed, 20 Jul 2011 02:43:38 -0700 (PDT)
Received: (qmail 5952 invoked by uid 514); 20 Jul 2011 09:43:14 -
Mailing-List: contact beginners-h...@perl.org; run by ezmlm
Precedence: bulk
List-Post: mailto:beginners@perl.org
List-Help: mailto:beginners-h...@perl.org
List-Unsubscribe: mailto:beginners-unsubscr...@perl.org
List-Subscribe: mailto:beginners-subscr...@perl.org
List-Id: beginners.perl.org
Delivered-To: mailing list beginners@perl.org
Received: (qmail 5937 invoked from network); 20 Jul 2011 09:43:14 -
Received: from x1.develooper.com (207.171.7.70)
  by x6.develooper.com with SMTP; 20 Jul 2011 09:43:14 -
Received: (qmail 24229 invoked by uid 225); 20 Jul 2011 09:43:13 -
Delivered-To: beginners@perl.org
Received: (qmail 24225 invoked by alias); 20 Jul 2011 09:43:13 -
X-Spam-Check-By: la.mx.develooper.com
Received: from gateway16.websitewelcome.com (HELO gateway16.websitewelcome.com)
 (69.56.162.7)
by la.mx.develooper.com (qpsmtpd/0.28) with SMTP; Wed, 20 Jul 2011
02:43:07 -0700
Received: (qmail 22012 invoked from network); 20 Jul 2011 09:41:24 -
Received: from gator245.hostgator.com (74.53.88.50)
  by gateway16.websitewelcome.com with SMTP; 20 Jul 2011 09:41:24 -
Received: from [79.183.124.234] (port=50803 helo=telaviv1.shlomifish.org)
by gator245.hostgator.com with esmtpa (Exim 4.69)
(envelope-from shlo...@shlomifish.org)
id 1QjTIy-00018D-Rf; Wed, 20 Jul 2011 04:43:01 -0500
Received: from telaviv1.shlomifish.org (telaviv1.shlomifish.org [127.0.0.1])
by telaviv1.shlomifish.org (Postfix) with ESMTP id D202911BD47;
Wed, 20 Jul 2011 12:42:45 +0300 (IDT)
Date: Wed, 20 Jul 2011 12:42:45 +0300
From: Shlomi Fish shlo...@shlomifish.org
To: Jim Gibson jimsgib...@gmail.com
Cc: beginners@perl.org
Subject: Re: redirect system command STDER
Message-ID: 20110720124245.0192b...@telaviv1.shlomifish.org
In-Reply-To: ca4b5fb6.13520%jimsgib...@gmail.com
References: 
CAKp6Vayo4JsCnPk2LkUgJ8EQ3wmNEqFC5LrNqkk9m8fkhsa=f...@mail.gmail.com

ca4b5fb6.13520%jimsgib...@gmail.com
X-Mailer: Claws Mail 3.7.9 (GTK+ 2.24.5; i586-mageia-linux-gnu)
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
X-AntiAbuse: This header was added to track abuse, please include it with
any abuse report
X-AntiAbuse: Primary Hostname - gator245.hostgator.com
X-AntiAbuse: Original Domain - perl.org
X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse: Sender Address Domain - shlomifish.org
X-BWhitelist: no
X-Source: 
X-Source-Args: 
X-Source-Dir: 
X-Source-Sender: bzq-79-183-124-234.red.bezeqint.net (telaviv1.shlomifish.org)
[79.183.124.234]:50803
X-Source-Auth: shlo...@shlomifish.org
X-Email-Count: 1
X-Source-Cap: c2hsb21pZjtzaGxvbWlmO2dhdG9yMjQ1Lmhvc3RnYXRvci5jb20=
Return-Path: beginners-return-117960-lellis=claimspages@perl.org
X-OriginalArrivalTime: 20 Jul 2011 09:43:48.0749 (UTC) FILETIME=[8695ABD0:01CC4
6C1]

Hi Jim,

On Tue, 19 Jul 2011 16:22:14 -0700
Jim Gibson jimsgib...@gmail.com wrote:

 On 7/19/11 Tue  Jul 19, 2011  12:14 PM, Tessio Fechine oiss...@gmail.com
 scribbled:
 
  Hello,
  I have a subroutine that uses useradd 

Re: help to print a hash with hash

2011-07-20 Thread Mohan L
How to insert below data structure in data base?

#!/usr/bin/perl

 use strict;
 use warnings;

 my %data = (

  south = {
  status = {
   open = { count = 3 },
   pws = { count = 3 },
   wip = { count = 0 },
   hold = { count = 1 },
   're-open' = { count = 0 },
   pwu = { count = 0 },
   openesc = { count = 0 },
  },
  total_count = 7,
  },

  north = {
  status = {
   open = { count = 3 },
   pws = { count = 0 },
   wip = { count = 0 },
   hold = { count = 7 },
   're-open' = { count = 0 },
   pwu = { count = 0 },
   openesc = { count = 0 },
  },
  total_count = 10,
  },
 );



I am using below code , but I think some thing going wrong here.

my @dbdata=();
for my $region ( sort keys %region_data )
{
my @mydata;
my $d = $region_data{ $region };
@mydata= join( ,, $region,map( $d-{ status }{ $_ }{ count },
@statuses_string ),$d-{ total_count },);
push(@dbdata,@mydata);
}

my $connect = DBI-connect($dsn, $username, $password);
my $query = INSERT INTO  location_wise(region,open,pws,wip
,hold,reopen,pwu,openesc,total) VALUES (?,?,?,?,?,?,?,?,?);
my $query_handle = $connect-prepare($query);

for my $datum (@dbdata) {
$query_handle-execute(@$datum);
}

DBD::mysql::st execute failed: Column 'open' cannot be null at
./demo.plline 104, CSV line 30.
DBD::mysql::st execute failed: Column 'open' cannot be null at
./demo.plline 104, CSV line 30.
DBD::mysql::st execute failed: Column 'open' cannot be null at
./demo.plline 104, CSV line 30.
DBD::mysql::st execute failed: Column 'open' cannot be null at
./demo.plline 104, CSV line 30.

I am hanging on this issue.  Any help will be really appreciated.


 @dbdata=(
 [north ,'3', '0' ,'0',  '7' ,'0' ,'0',' 0',' 10'],
 [south, '3',' 3' ,'0' ,'1', '0',' 0' ,'0', '7' ],
 )
 my $connect = DBI-connect($dsn, $username, $password);
 my $query = INSERT INTO  by_region (region,open,pws,wip
 ,hold,reopen,pwu,openesc,total) VALUES (?,?,?,?,?,?,?,?,?);

 my $query_handle = $connect-prepare($query);
 for my $datum (@dbdata)
 {
 $query_handle-execute(@$datum);
 }




Re: help to print a hash with hash

2011-07-20 Thread shawn wilson
On Wed, Jul 20, 2011 at 10:41, Mohan L l.mohan...@gmail.com wrote:


 DBD::mysql::st execute failed: Column 'open' cannot be null at
 ./demo.plline 104, CSV line 30.
 DBD::mysql::st execute failed: Column 'open' cannot be null at
 ./demo.plline 104, CSV line 30.
 DBD::mysql::st execute failed: Column 'open' cannot be null at
 ./demo.plline 104, CSV line 30.
 DBD::mysql::st execute failed: Column 'open' cannot be null at
 ./demo.plline 104, CSV line 30.

 I am hanging on this issue.  Any help will be really appreciated.


not sure what your table looks like. however, if you want to be able
to not have any data in a column, you need to allow the column to be
null. you should also do something like:
$data = $column //= ;

also, since your data is already a hash, you could just use dbic and
define a resultset to allow your data to be imported instead of
refactoring it.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help to print a hash with hash

2011-07-20 Thread Mohan L
On Wed, Jul 20, 2011 at 9:23 PM, shawn wilson ag4ve...@gmail.com wrote:

 On Wed, Jul 20, 2011 at 10:41, Mohan L l.mohan...@gmail.com wrote:

 
  DBD::mysql::st execute failed: Column 'open' cannot be null at
  ./demo.plline 104, CSV line 30.
  DBD::mysql::st execute failed: Column 'open' cannot be null at
  ./demo.plline 104, CSV line 30.
  DBD::mysql::st execute failed: Column 'open' cannot be null at
  ./demo.plline 104, CSV line 30.
  DBD::mysql::st execute failed: Column 'open' cannot be null at
  ./demo.plline 104, CSV line 30.
 
  I am hanging on this issue.  Any help will be really appreciated.
 

 not sure what your table looks like. however, if you want to be able
 to not have any data in a column, you need to allow the column to be
 null. you should also do something like:
 $data = $column //= ;


for my $datum (@dbdata)
{
   print $datum\n;
}

Output :

east,0,0,2,4,0,0,0,6
north,0,0,7,3,0,0,0,10
south,3,0,1,3,0,0,0,7
west,7,0,0,0,0,0,0,7

But the below code does not work :

for my $datum (@dbdata) {
$query_handle-execute(@$
datum);
}


 also, since your data is already a hash, you could just use dbic and
 define a resultset to allow your data to be imported instead of
 refactoring it.


How to do that using dbic?  any example will help.

Thanks  Rg
Mohan L


Re: help to print a hash with hash

2011-07-20 Thread Jim Gibson
On 7/20/11 Wed  Jul 20, 2011  9:04 AM, Mohan L l.mohan...@gmail.com
scribbled:

 On Wed, Jul 20, 2011 at 9:23 PM, shawn wilson ag4ve...@gmail.com wrote:
 
 On Wed, Jul 20, 2011 at 10:41, Mohan L l.mohan...@gmail.com wrote:
 
 
 DBD::mysql::st execute failed: Column 'open' cannot be null at
 ./demo.plline 104, CSV line 30.
 DBD::mysql::st execute failed: Column 'open' cannot be null at
 ./demo.plline 104, CSV line 30.
 DBD::mysql::st execute failed: Column 'open' cannot be null at
 ./demo.plline 104, CSV line 30.
 DBD::mysql::st execute failed: Column 'open' cannot be null at
 ./demo.plline 104, CSV line 30.
 
 I am hanging on this issue.  Any help will be really appreciated.
 
 
 not sure what your table looks like. however, if you want to be able
 to not have any data in a column, you need to allow the column to be
 null. you should also do something like:
 $data = $column //= ;
 
 
 for my $datum (@dbdata)
 {
print $datum\n;
 }
 
 Output :
 
 east,0,0,2,4,0,0,0,6
 north,0,0,7,3,0,0,0,10
 south,3,0,1,3,0,0,0,7
 west,7,0,0,0,0,0,0,7

If that is really the output from the print statement shown above, then you
don't have a rows of column data in your @dbdata array elements, you have an
array of scalar string values that are the concatenated values of the
desired column values joined with commas.

Use the Data::Dumper module to see what is really contained within your data
array:

use Data::Dumper;
...
print Dumper(\@dbdata);


 
 But the below code does not work :
 
 for my $datum (@dbdata) {
 $query_handle-execute(@$
 datum);
 }

That assumes that the elements of @dbdata are references to arrays. Printing
the array using the Data::Dumper module will tell you if that is so.

If the elements are really 'east,0,0,2,4,0,0,0,6' and the like, then you are
probably trying to store that string into the first column and nothing into
the subsequent columns. That might explain why you are getting column
cannot be null error messages.

You could split the scalar into columns:

my @row = split(/,/,$dbdata);

but you should probably go back and figure out why @dbdata contains the data
that it does. 

If you want more help, please post a short, complete program that
demonstrates the problem you are having, and one that other people can
execute on their own systems.

Thanks.



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Regular Expression change

2011-07-20 Thread Rob Dixon

On 15/07/2011 16:42, David Wagner wrote:


I have the following map:

map{[$_,(/^\d/ ? 1 : 0) . /^([^;]+)/,  
/[^;]+;[^;]*;[^;]+;[^;]+;([^;]+);/]}

I had a failure during the night because some data field(s) had
a semi-colon in the data. So what I have is a pre-defined data separator
that would not normally appear in data. What I have selected and have
been using is ;';  . I was going to do this, until I got down to this
map and I am unsure how to change ([^;]+) or [^;]+ to have ;'; as the
separator of my fields. What I am doing is reports and scrapping the
data, collecting and then reformatting to send out as emails.

Any thoughts on what could be done??

   Thanks for any insights you might on this...

Wags ;)


Hello David.

Fiest of all, setting aside your embedded field separators, may I make
some comments on your code?

- I find it a little impregnable, and think you could make it more
readable by assing some whitespace.

- The second element of your anonymous array seems a little strange, but
it looks like you want the first field in the data, preceded by '1' or
'0' according to whether it starts with a digit. But your regex is in
scalar context so, instead of extracting the first field, you will get
'1' or '' according to the success of the match. To extract the value of
the field itself you must apply list context - something like

  (/^\d/ ? 1 : 0) . (/^([^;]+)/)[0]

- The regex generating the third field can be written more readably as

  / (?: [^;]+ ;){4} ([^;]+); /x

So as a first improvement I suggest

  map { [
$_,
(/^\d/ ? 1 : 0) . (/^([^;]+)/)[0],
/ (?: [^;]+ ;){4} ([^;]+); /x
  ] }

But I think it would be best to use split rather than regexes to first
separate the data into fields and then manipulate them individually.

map {
  my @fields = split /;/;
  [
$_,
($fields[0] =~ /^\d/ ? 1 : 0) . $fields[0],
$fields[4]
  ]
}

Finally, to handle the embedded semicolons properly, simply replace the
split with a call to Text::CSV as Ruud recommends. Without knowing how
your data distinguishes between separators and data I cannot be sure how
this should be coded, but by default the module assumes double-quotes
around fields that must not be split.

  use Text::CSV;

  my $csv = Text::CSV-new({sep_char = ';'});

  map {
$csv-parse($_) or die $csv-error_diag;
my @fields = $csv-fields;
[
  $_,
  ($fields[0] =~ /^\d/ ? 1 : 0) . $fields[0],
  $fields[4]
]
  }

One last thought - I think map is probably a poor choice in this case,
but I cannot tell from only a fragment of your code. I would prefer to
see a 'foreach' or a 'while iterating over the source data, and the
corresponding translation pushed onto a target array.

I hope this helps,

Rob

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help to print a hash with hash

2011-07-20 Thread Mohan L
  east,0,0,2,4,0,0,0,6
  north,0,0,7,3,0,0,0,10
  south,3,0,1,3,0,0,0,7
  west,7,0,0,0,0,0,0,7

 If that is really the output from the print statement shown above, then you
 don't have a rows of column data in your @dbdata array elements, you have
 an
 array of scalar string values that are the concatenated values of the
 desired column values joined with commas.


Thanks . Your are correct .


 Use the Data::Dumper module to see what is really contained within your
 data
 array:

 use Data::Dumper;
 ...
 print Dumper(\@dbdata);


-Output ---
$VAR1 = [
  'east,0,0,2,4,0,0,0,6',
  'north,0,0,7,3,0,0,0,10',
  'south,3,0,1,3,0,0,0,7',
  'west,7,0,0,0,0,0,0,7'
];



 That assumes that the elements of @dbdata are references to arrays.
 Printing
 the array using the Data::Dumper module will tell you if that is so.

 If the elements are really 'east,0,0,2,4,0,0,0,6' and the like, then you
 are
 probably trying to store that string into the first column and nothing into
 the subsequent columns. That might explain why you are getting column
 cannot be null error messages.

 You could split the scalar into columns:

 my @row = split(/,/,$dbdata);

 but you should probably go back and figure out why @dbdata contains the
 data
 that it does.

 If you want more help, please post a short, complete program that
 demonstrates the problem you are having, and one that other people can
 execute on their own systems.

 Thanks.


I have structure below  which I want to insert into MySQL Data base.

my %data = (

 south = {
 status = {
  open = { count = 3 },
  pws = { count = 3 },
  wip = { count = 0 },
  hold = { count = 1 },
  're-open' = { count = 0 },
  pwu = { count = 0 },
  openesc = { count = 0 },
 },
 total_count = 7,
 },

 north = {
 status = {
  open = { count = 3 },
  pws = { count = 0 },
  wip = { count = 0 },
  hold = { count = 7 },
  're-open' = { count = 0 },
  pwu = { count = 0 },
  openesc = { count = 0 },
 },
 total_count = 10,
 },
);

I am using below code to insert database :

my @dbdata=();
for my $region ( sort keys %region_data )
{
my @mydata;
my $d = $region_data{ $region };
@mydata= join( ,, $region,map( $d-{ status }{ $_ }{ count },
@statuses_string ),$d-{ total_count },);
push(@dbdata,@mydata);

}

my $connect = DBI-connect($dsn, $username, $password);
my $query = INSERT INTO  location_wise(region,open,pws,
wip  ,hold,reopen,pwu,openesc,total) VALUES (?,?,?,?,?,?,?,?,?);

my $query_handle = $connect-prepare($query);
for my $datum (@dbdata) {
$query_handle-execute(@$datum);
}

It shows below error :
DBD::mysql::st execute failed: Column 'open' cannot be null at /demo.plline
104, CSV line 30.

From Jim Gibson :

print Dumper(\@dbdata);
$VAR1 = [
  'east,0,0,2,4,0,0,0,6',
  'north,0,0,7,3,0,0,0,10',
  'south,3,0,1,3,0,0,0,7',
  'west,7,0,0,0,0,0,0,7'
];

It trying to store that string into the first column and nothing into the
subsequent columns. That is why it  shows column cannot be null error
messages. Thanks Jim Gibson!!!.

Jim Gibson, I understand the problem, but I not sure where in the below code
I am making mistake .

my @dbdata=();
for my $region ( sort keys %region_data )
{
my @mydata;
my $d = $region_data{ $region };
@mydata= join( ,, $region,map( $d-{ status }{ $_ }{ count },
@statuses_string ),$d-{ total_count },);
push(@dbdata,@mydata);

}

Thanks  Rg
Mohan L


Increment UID Field

2011-07-20 Thread Overkill

Greetings,

I'm trying to increment the UID field of the unix password file from an 
csv file.  I've tried to insert C style increment and it keeps bomping 
out.  What would be the logic to increment the 5009 to increment by 
one?  Thanks for any help.


-Overkill

#!/usr/bin/perl

#use strict;
#use warnings;

   while (DATA) {
   chomp;
($first, $last, $username) = split(,);
  print $username:x:5009:4001:$first 
$last:/home/$username:/bin/false\n;

  }
  exit;

__DATA__
Bob,Ahrary,bahrary
Jill,Anderson,janderson


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help to print a hash with hash

2011-07-20 Thread Brandon McCaig
Hello:

On Wed, Jul 20, 2011 at 12:45 PM, Mohan L l.mohan...@gmail.com wrote:
 It trying to store that string into the first column and nothing into
 the subsequent columns. That is why it  shows column cannot be null
 error messages. Thanks Jim Gibson!!!.

 Jim Gibson, I understand the problem, but I not sure where in the below
 code I am making mistake .

I'm not sure what you're trying to do there. :P The 'join' seems
to be the root of the problem, but I'm not even going to attempt
to figure it out.

You need to turn a hash into an array (reference). Simplest way
is perhaps a hash slice.

use strict;
use warnings;

use Data::Dumper;

my %data = (
south = {
status = {
open = { count = 3 },
pws = { count = 3 },
wip = { count = 0 },
hold = { count = 1 },
're-open' = { count = 0 },
pwu = { count = 0 },
openesc = { count = 0 },
},
total_count = 7,
},

north = {
status = {
open = { count = 3 },
pws = { count = 0 },
wip = { count = 0 },
hold = { count = 7 },
're-open' = { count = 0 },
pwu = { count = 0 },
openesc = { count = 0 },
},
total_count = 10,
},
);

# This will be the data to execute the query with.
my @rows;

for my $region (keys %data)
{
my $status = $data{$region}-{status};

# Slice of hash referenced by $status.
my @statuses = @{$status}{qw(
open pws wip hold re-open pwu openesc)};

# Map each status hash to the count inside.
my @counts = map { $_-{count} } @statuses;

# Create an array reference of the row, with the region name,
# counts, and total.
my $row = [$region, @counts, $data{$region}-{total_count}];

push @rows, $row;
}

print Data::Dumper-Dump([\@rows], ['rows']);

__END__

Output:

$rows = [
  [
'south',
3,
3,
0,
1,
0,
0,
0,
7
  ],
  [
'north',
3,
0,
0,
7,
0,
0,
0,
10
  ]
];


HTH.


-- 
Brandon McCaig http://www.bamccaig.com/bamcc...@gmail.com
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software http://www.castopulence.org/bamcc...@castopulence.org

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Increment UID Field

2011-07-20 Thread Rob Coops
On Wed, Jul 20, 2011 at 7:24 PM, Overkill overk...@sadiqs.net wrote:

 Greetings,

 I'm trying to increment the UID field of the unix password file from an csv
 file.  I've tried to insert C style increment and it keeps bomping out.
  What would be the logic to increment the 5009 to increment by one?  Thanks
 for any help.

 -Overkill

 #!/usr/bin/perl

 #use strict;
 #use warnings;

   while (DATA) {
   chomp;
($first, $last, $username) = split(,);
  print $username:x:5009:4001:$first $last:/home/$username:/bin/**
 false\n;
  }
  exit;

 __DATA__
 Bob,Ahrary,bahrary
 Jill,Anderson,janderson


 --
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/


 #!/usr/bin/perl

use strict;
use warnings;

my $userid = 5009;
  while (DATA) {
  chomp;
   ($first, $last, $username) = split(,);
 print $username:x:$userid:4001:$first $last:/home/$username:/bin/**
false\n;
 $userid++;
 }
 exit;

__DATA__
Bob,Ahrary,bahrary
Jill,Anderson,janderson

That should do the trick... you could go for a: print $username:x: .
$userid++ . :4001:$first $last:/home/$username:/bin/**false\n; as well if
you like to keep it shorter. or change $userid++; to $userid +1; or even
$userid = $userid +1; depending on your personal preference. The result is
adding 1 to the $userid variable which is what you are looking to do :-)

Regards,

Rob


Re: Increment UID Field

2011-07-20 Thread Overkill

Rob, great ideas and worked like a charm. Thanks again,
Overkill.

On 07/20/2011 01:42 PM, Rob Coops wrote:

On Wed, Jul 20, 2011 at 7:24 PM, Overkilloverk...@sadiqs.net  wrote:


Greetings,

I'm trying to increment the UID field of the unix password file from an csv
file.  I've tried to insert C style increment and it keeps bomping out.
  What would be the logic to increment the 5009 to increment by one?  Thanks
for any help.

-Overkill

#!/usr/bin/perl

#use strict;
#use warnings;

   while (DATA) {
   chomp;
($first, $last, $username) = split(,);
  print $username:x:5009:4001:$first $last:/home/$username:/bin/**
false\n;
  }
  exit;

__DATA__
Bob,Ahrary,bahrary
Jill,Anderson,janderson


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


#!/usr/bin/perl

use strict;
use warnings;

my $userid = 5009;
   while (DATA) {
   chomp;
($first, $last, $username) = split(,);
  print $username:x:$userid:4001:$first $last:/home/$username:/bin/**
false\n;
  $userid++;
  }
  exit;

__DATA__
Bob,Ahrary,bahrary
Jill,Anderson,janderson

That should do the trick... you could go for a: print $username:x: .
$userid++ . :4001:$first $last:/home/$username:/bin/**false\n; as well if
you like to keep it shorter. or change $userid++; to $userid +1; or even
$userid = $userid +1; depending on your personal preference. The result is
adding 1 to the $userid variable which is what you are looking to do :-)

Regards,

Rob




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Increment UID Field

2011-07-20 Thread Shlomi Fish
Hi Overkill,

On Wed, 20 Jul 2011 13:24:06 -0400
Overkill overk...@sadiqs.net wrote:

 Greetings,
 
 I'm trying to increment the UID field of the unix password file from an 
 csv file.  I've tried to insert C style increment and it keeps bomping 
 out.  What would be the logic to increment the 5009 to increment by 
 one?  Thanks for any help.
 
 -Overkill
 

don't parse CSV using regular expressions - use Text::CSV :

http://perl-begin.org/uses/text-parsing/

Regards,

Shlomi Fish

 #!/usr/bin/perl
 
 #use strict;
 #use warnings;
 
 while (DATA) {
 chomp;
  ($first, $last, $username) = split(,);
print $username:x:5009:4001:$first 
 $last:/home/$username:/bin/false\n;
}
exit;
 
 __DATA__
 Bob,Ahrary,bahrary
 Jill,Anderson,janderson
 
 



-- 
-
Shlomi Fish   http://www.shlomifish.org/
What Makes Software Apps High Quality -  http://shlom.in/sw-quality

The prefix “God Said” has the extraordinary logical property of converting any
statement that follows it into a true one.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Increment UID Field

2011-07-20 Thread Rob Coops
On Wed, Jul 20, 2011 at 9:53 PM, Shlomi Fish shlo...@shlomifish.org wrote:

 Hi Overkill,

 On Wed, 20 Jul 2011 13:24:06 -0400
 Overkill overk...@sadiqs.net wrote:

  Greetings,
 
  I'm trying to increment the UID field of the unix password file from an
  csv file.  I've tried to insert C style increment and it keeps bomping
  out.  What would be the logic to increment the 5009 to increment by
  one?  Thanks for any help.
 
  -Overkill
 

 don't parse CSV using regular expressions - use Text::CSV :

 http://perl-begin.org/uses/text-parsing/

 Regards,

Shlomi Fish

  #!/usr/bin/perl
 
  #use strict;
  #use warnings;
 
  while (DATA) {
  chomp;
   ($first, $last, $username) = split(,);
 print $username:x:5009:4001:$first
  $last:/home/$username:/bin/false\n;
 }
 exit;
 
  __DATA__
  Bob,Ahrary,bahrary
  Jill,Anderson,janderson
 
 



 --
 -
 Shlomi Fish   http://www.shlomifish.org/
 What Makes Software Apps High Quality -  http://shlom.in/sw-quality

 The prefix “God Said” has the extraordinary logical property of converting
 any
 statement that follows it into a true one.

 Please reply to list if it's a mailing list post - http://shlom.in/reply .

 --
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/



I'm willing to dispute that. :-)

For the simple reason that as long as you control the input to the CVS file
and can be certain that no UTF8 characters or any other mess sneaks in the
simple split using the /,/ regex will be faster and much simpler then the
Text::CVS module though in any situation where you do not control the input
or in the future might not have control over the input you should definitely
use it.

Based on the current data set however this module is pure overkill...

There is a fine line between a module being useful and being over kill if
you are creating a script that you might want to use in the future or share
with colleagues etc... then you should definitely use modules as usually
they cover all kinds of corner cases that you are unlikely to even think of
let alone cover in your code. But there are situations where that is simply
not needed. A simple example is a script I once wrote for an ISP to deal
with a cable company reorganizing their network, the input though not
controlled by me was bound by a lot of rules and I could without any worries
assume all kinds of things about my input based on the business rules
governing the naming of nodes bundles and locations in the network. Of
course these situations are not very common but in case you do encounter
such a situation it can be very handy to ignore the modules and go for a
much more limited but much more strict way of working that throws an error
on every corner case that should not happen based on the business rules. (as
long as you are prepared to get called out of bed at 3 at night when an
operator finds informs you that due to a decission made a little change in
the naming conventions and your script is rolling over and playing dead
because it was not informed about this...)

In general I do agree wit the usage of modules since in the end the modules
normally have seen many iterations and usually cover much more situations
the the once you can think of based on your typical working situaiton...
which means you are much more certain your script will be able to survive
even if in a few years form now you company moves to China and wants to use
chineese names rather then latin names; which would most likely break your
current script by the way :-)

Regards,

Rob


Subdirectories and use lib

2011-07-20 Thread Marc
I'd like to organize a project's files into subdirectories for better 
housekeeping.  I thought that use lib .; would also include any 
subdirectories, but sadly it doesn't.  Is there a way to include all subdirs 
without having a list a mile long like this?
use lib /Applications/MAMP/cgi-bin/cart/sql/lib/Module/;
use lib /Applications/MAMP/cgi-bin/cart/sql/lib/Params/;
use lib /Applications/MAMP/cgi-bin/cart/sql/Business/CreditFraud/;
etc...

Or am I going about it the wrong way?

Thanks,
Marc
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Arrow Notation vs. Colon Notation

2011-07-20 Thread Marc
I've noticed that the following two lines seem to be equivalent when 
calling a sub from a module called Lib.pm:

Lib-load_config(config.dat);

Lib::load_config(config.dat);

Is this just a case of TIMTOWTDI or is there a difference in how they 
do what they do?

Marc
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Arrow Notation vs. Colon Notation

2011-07-20 Thread Uri Guttman
 M == Marc  sono...@fannullone.us writes:

  MI've noticed that the following two lines seem to be equivalent when 
calling a sub from a module called Lib.pm:
  Lib- load_config(config.dat);

  M Lib::load_config(config.dat);

  MIs this just a case of TIMTOWTDI or is there a difference in how
  Mthey do what they do?

they are actually very different. the :: call is just a plain sub call
with a fully qualified path. if the sub was imported into the current
package you wouldn't even need the Lib:: part.

the other is a class method call. it has two major differences. first it
will pass its class (or object, the arg before -) as the first arg in
the call. the second thing is that it will search the class hierarchy
(using the package global's @ISA) to find that method if it isn't in the
Lib class.

so don't think they are even similar as one is a sub call and the other
is a class method call. they aren't interchangeble at all

uri

-- 
Uri Guttman  --  uri AT perlhunter DOT com  ---  http://www.perlhunter.com --
  Perl Developer Recruiting and Placement Services  -
-  Perl Code Review, Architecture, Development, Training, Support ---

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: XML suggestions

2011-07-20 Thread Jenda Krynicky
From:   Rob Coops rco...@gmail.com
 It really depends on what you are looking to do though, if you are aiming
 for just a simple thing with only a few messages then don't worry
 about XML::LibXML
 and go for XML::Simple which is more then enough in most simple cases ;-)

Except that it's not so simple to set it up to give you consistent 
data structure with things like optional tag attributes and repeated 
tags. See http://www.perlmonks.org/?node_id=697036

 I found that both Twig and Treebuilder are great if you are looking at
 modifying the XML (add extra nodes, alter existing nodes) but since they
 build a full tree structure for the XML file you are working with depending
 on the size and complexity of the file this could cause serious memory
 bloat, and even out of memory errors.

XML::Twig (as the name suggests) is not supposed to build a full tree 
structure, but rather lets you work with parts of the document. The 
twigs.

I personally believe XML::LibXML is overcomplicated, but some people 
seem to like it.

Jenda
= je...@krynicky.cz === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Arrow Notation vs. Colon Notation

2011-07-20 Thread Marc
On Jul 20, 2011, at 4:03 PM, Uri Guttman wrote:

 so don't think they are even similar as one is a sub call and the other
 is a class method call. they aren't interchangeble at all

Yep, you're correct.  Why is everything so simple once someone explains 
it to you???  =;)

That also cleared up a strange problem I was having with my code.  Boy, 
am I embarrassed.  On the plus side, this ordeal has helped to solidify it for 
me, so I guess it's O.K. to make stupid mistakes once in awhile, no?

Thanks, Uri.

Marc
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Arrow Notation vs. Colon Notation

2011-07-20 Thread jbiskofski
Marc that was a really good question. On the surface they do seem exactly 
alike. 

Cheers!

- biskofski

On Jul 20, 2011, at 6:29 PM, Marc sono...@fannullone.us wrote:

 On Jul 20, 2011, at 4:03 PM, Uri Guttman wrote:
 
 so don't think they are even similar as one is a sub call and the other
 is a class method call. they aren't interchangeble at all
 
Yep, you're correct.  Why is everything so simple once someone explains it 
 to you???  =;)
 
That also cleared up a strange problem I was having with my code.  Boy, am 
 I embarrassed.  On the plus side, this ordeal has helped to solidify it for 
 me, so I guess it's O.K. to make stupid mistakes once in awhile, no?
 
 Thanks, Uri.
 
 Marc
 -- 
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/
 
 

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Arrow Notation vs. Colon Notation

2011-07-20 Thread Shawn H Corey

On 11-07-20 07:03 PM, Uri Guttman wrote:

the other is a class method call. it has two major differences. first it
will pass its class (or object, the arg before -) as the first arg in
the call. the second thing is that it will search the class hierarchy
(using the package global's @ISA) to find that method if it isn't in the
Lib class.


Not true in all cases:

#!/usr/bin/env perl

use strict;
use warnings;

package Foo;
sub foo {
  print foo\n;
}

package main;

Foo::foo();
Foo-foo();

Foo::bar();

__END__

The difference here is that `Foo:bar()` gives a run-time error; where 
`Foo-bar()` gives a compile-time error.


Compile-time errors are always reported.  Run-time errors are only 
reported if the subroutine is called.  If you have something like this, 
the script would only stop for some rare condition:


if( come_rare_condition ){
  Foo::bar();
}

Not something you want to discover after the code is in production.  :)


--
Just my 0.0002 million dollars worth,
  Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software:  Fail early  often.

Eliminate software piracy:  use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: redirect system command STDER

2011-07-20 Thread C.DeRykus
On Jul 20, 12:45 am, walde.christ...@googlemail.com (Christian
Walde) wrote:
 On Tue, 19 Jul 2011 21:14:10 +0200, Tessio Fechine oiss...@gmail.com wrote:
  Hello,
  I have a subroutine that uses useradd to create accounts

  --
  @cmd = ('useradd', '-m', $account);
  my $result = system @cmd;
  --

  but when useradd fails, I need to stop it from sending the error message to
  STDER.
  Is it possible with system?

  Thanks!

 For an easy and cross-platform solution you can use Capture::Tiny to capture 
 both STDERR and STDOUT separately and then have your perl code decide what to 
 do with each.


Neat solution.  (I wonder why the name 'Tiny' rather than...
say,  IPC::Capture  for instance..)

Another possibility would IPC::Run :

  use IPC::Run qw( run timeout );

  run \@cmd, \$in, \$out, \$err, timeout( 10 )
or die cmd err: $?;

--
Charles DeRykus




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




first post

2011-07-20 Thread H Kern
Hi, My first newbie post. I wish to have two arrays indexed by a hash  
table. The simple program below runs and behaves properly initializing the  
hash table with the information I wish it to have.


However, Perl generates the following suggestion on the @header{}  
assignment statements,


  Scalar value @header{keys} better written as $header{keys} at  
iifm.pl line...


If I rewrite it as Perl suggests, the two %header{} elements get  
initialized to the size of the arrays instead of the arrays. Why does Perl  
make this suggestion, and how do I get rid of it without getting rid of  
the use warnings statement?


Thanks, --H



use strict;
use warnings;
my %header;

open( IN, , $ARGV[0] );

@header{keys} = split(/\t\n/, IN);
@header{info} = split(/\t\n/, IN);



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: first post

2011-07-20 Thread Shawn H Corey

On 11-07-20 10:28 PM, H Kern wrote:

use strict;
use warnings;
my %header;

open( IN, , $ARGV[0] );

@header{keys} = split(/\t\n/, IN);
@header{info} = split(/\t\n/, IN);


I'm not sure but I think this is what you want:

( $header{keys}, $header{info} ) = split(/\t\n/, IN);


Also, Data::Dumper will dump the complex structures so you can see 
exactly what it is.  Comment it out to stop its output.


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

Data::Dumper is a standard module and is installed with Perl.  For a 
list of all the standard modules and pragmatics, see `perldoc perlmodlib`.



--
Just my 0.0002 million dollars worth,
  Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software:  Fail early  often.

Eliminate software piracy:  use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Arrow Notation vs. Colon Notation

2011-07-20 Thread Rob Dixon

On 21/07/2011 02:09, Shawn H Corey wrote:

On 11-07-20 07:03 PM, Uri Guttman wrote:

the other is a class method call. it has two major differences. first it
will pass its class (or object, the arg before -) as the first arg in
the call. the second thing is that it will search the class hierarchy
(using the package global's @ISA) to find that method if it isn't in the
Lib class.


Not true in all cases:

#!/usr/bin/env perl

use strict;
use warnings;

package Foo;
sub foo {
print foo\n;
}

package main;

Foo::foo();
Foo-foo();

Foo::bar();

__END__

The difference here is that `Foo:bar()` gives a run-time error; where
`Foo-bar()` gives a compile-time error.

Compile-time errors are always reported. Run-time errors are only
reported if the subroutine is called. If you have something like this,
the script would only stop for some rare condition:

if( come_rare_condition ){
Foo::bar();
}

Not something you want to discover after the code is in production. :)


I'm not sure what you mean here Shawn. Errors are generated at run time
in both cases, so code like

  if (0) {
Foo::bar();
  }
  if (0) {
Foo-bar();
  }

will generate no errors at all. In particular the method call cannot
generate a compile-time error as Perl supports dynamic binding in order
to implement object-orientation.

It would be possible to generate calls to undefined subroutines at
compile time, but because the symbol table can be modified at run time
with such trickery suck as *Foo::bar = \Foo::foo it is also left until
run time to report any such errors.

Rob

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




second post

2011-07-20 Thread H Kern

Just a followup on that last one...

I'm using this code thinking that ($header{info}) will be an array of all  
the elements in the string that was read in.


The first print statement does what I hope, printing out the elements of  
that array as a string.


But, when the perl compiler gets to the second print statement, it balks.

Can't use string (HDR  QuickBooks Premier  Version 2...) as an  
ARRAY ref while strict refs in use at...


This makes me think that in the fourth line '( $header{info} ) =  
split(/\t\n/, );'  $header{info} is actually getting set to a string  
of the concatenation of the parts split apart by the split function. Is  
this true?


If so, then how do I set the hash table element header{info} to be the  
array of elements created by the split() function?


... and, can the elements of that array be accessed using the syntax I use  
in the second print statement?


Thanks, --H

code:

my %header;

( $header{keys} ) = split(/\t\n/, );
( $header{info} ) = split(/\t\n/, );

print $header{info} . \n;
print $header{info}[0] . \n;

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: second post

2011-07-20 Thread jbiskofski
If you want to store an array inside a hash value you have to store a reference 
to it, not the actual array. Here are two examples :

my %hash;
my @arr = split(...);
$hash{'key'} = \@arr;

Or shorter :

$hash{'key'} = [ split(...) ];

Cheers dude!

-- biskofski

Sent from my iPad

On  Jul 20, 2011, at 11:34 PM, H Kern g...@pbwe.com wrote:

 Just a followup on that last one...
 
 I'm using this code thinking that ($header{info}) will be an array of all 
 the elements in the string that was read in.
 
 The first print statement does what I hope, printing out the elements of that 
 array as a string.
 But, when the perl compiler gets to the second print statement, it balks.
 
 Can't use string (HDR  QuickBooks Premier  Version 2...) as an ARRAY 
 ref while strict refs in use at...
 This makes me think that in the fourth line '( $header{info} ) = 
 split(/\t\n/, );'  $header{info} is actually getting set to a string of 
 the concatenation of the parts split apart by the split function. Is this 
 true?
 
 If so, then how do I set the hash table element header{info} to be the 
 array of elements created by the split() function?
 
 ... and, can the elements of that array be accessed using the syntax I use in 
 the second print statement?
 
 Thanks, --H
 
 code:
 
 my %header;
 
 ( $header{keys} ) = split(/\t\n/, );
 ( $header{info} ) = split(/\t\n/, );
 
 print $header{info} . \n;
 print $header{info}[0] . \n;
 
 -- 
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/
 
 

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Arrow Notation vs. Colon Notation

2011-07-20 Thread Uri Guttman
 RD == Rob Dixon rob.di...@gmx.com writes:

  RD I'm not sure what you mean here Shawn. Errors are generated at run time
  RD in both cases, so code like

  RD   if (0) {
  RD Foo::bar();
  RD   }
  RD   if (0) {
  Foo- bar();
  RD   }

  RD will generate no errors at all. In particular the method call cannot
  RD generate a compile-time error as Perl supports dynamic binding in order
  RD to implement object-orientation.

what he said. there is no compile time error in shawn's code.

uri

-- 
Uri Guttman  --  uri AT perlhunter DOT com  ---  http://www.perlhunter.com --
  Perl Developer Recruiting and Placement Services  -
-  Perl Code Review, Architecture, Development, Training, Support ---

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: first post

2011-07-20 Thread Uri Guttman
 SHC == Shawn H Corey shawnhco...@gmail.com writes:

  SHC On 11-07-20 10:28 PM, H Kern wrote:
   use strict;
   use warnings;
   my %header;
   
   open( IN, , $ARGV[0] );
   
   @header{keys} = split(/\t\n/, IN);
   @header{info} = split(/\t\n/, IN);

  SHC I'm not sure but I think this is what you want:

  SHC ( $header{keys}, $header{info} ) = split(/\t\n/, IN);

nope. he is splitting two different input lines and you read in one
line.

what perl is saying is that @header{'keys'} is a literal single index in
a slice which is just wrong. a slice needs multiple keys or an array for
the index and then perl thinks it is fine.

the next problem is that $header{'keys'} is a scalar value now and split
expects to be assigned to a list. so the solution is to make a list:

( $header{keys} ) = split(/\t\n/, IN);

that way perl keeps quiet as the index and sigil are in agreement. the
() make a list so only the first value of the split will be assigned to
the hash element. everyone is happy!

uri

-- 
Uri Guttman  --  uri AT perlhunter DOT com  ---  http://www.perlhunter.com --
  Perl Developer Recruiting and Placement Services  -
-  Perl Code Review, Architecture, Development, Training, Support ---

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: second post

2011-07-20 Thread Rob Dixon

On 21/07/2011 05:34, H Kern wrote:

Just a followup on that last one...

I'm using this code thinking that ($header{info}) will be an array of
all the elements in the string that was read in.

The first print statement does what I hope, printing out the elements of
that array as a string.

But, when the perl compiler gets to the second print statement, it balks.

Can't use string (HDR QuickBooks Premier Version 2...) as an ARRAY
ref while strict refs in use at...

This makes me think that in the fourth line '( $header{info} ) =
split(/\t\n/, );' $header{info} is actually getting set to a string
of the concatenation of the parts split apart by the split function. Is
this true?

If so, then how do I set the hash table element header{info} to be the
array of elements created by the split() function?

... and, can the elements of that array be accessed using the syntax I
use in the second print statement?

Thanks, --H

code:

my %header;

( $header{keys} ) = split(/\t\n/, );
( $header{info} ) = split(/\t\n/, );

print $header{info} . \n;
print $header{info}[0] . \n;


You are misinterpreting what your code is doing. The values of a hash
can only be scalar values (string, number, reference or undef). The
split call is splitting the input line wherever there is a tab followed
by a newline, which is probably nowhere so you are compying the entire
input line as an unbroken string to the hash.

It is common practice to remove the trailing newline from an input
record using chomp, after which you need only split on tabs.

However, because the hash will only take a scalar, it will not hold a
list of values and instead you will have to build an anonymous array and
put a reference to it in the hash, like this:

  my $line = ;
  chomp $line;
  $header{keys} = [ split /\t/, $line ];

  $line = ;
  chomp $line;
  $header{info} = [ split /\t/, $line ];

Or, more concisely

  for my $key ('keys', 'info') {
my $line = ;
chomp $line;
$header{$key} = [ split /\t/, $line ];
  }

and then your second print statement will work as it is. The first,
however, would have to be written as something like

  print @$header{info}\n;

The whole issue if references is not a simple one, and you have chosen a
non-trivial task to start programmin Perl. The whole story is laid out
in the documentation in

  perldoc perlreftut

and

  perldoc prelref

Good luck!

Rob


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: second post

2011-07-20 Thread Uri Guttman
 HK == H Kern g...@pbwe.com writes:

  HK Just a followup on that last one...
  HK I'm using this code thinking that ($header{info}) will be an array of
  HK all  the elements in the string that was read in.

  HK But, when the perl compiler gets to the second print statement, it balks.

  HK Can't use string (HDR  QuickBooks Premier  Version 2...) as an
  HK ARRAY ref while strict refs in use at...

  HK This makes me think that in the fourth line '( $header{info} ) =
  HK split(/\t\n/, );'  $header{info} is actually getting set to a
  HK string  of the concatenation of the parts split apart by the split
  HK function. Is  this true?

you are assigning a single value from the split, not an array. you can't
assign arrays in perl to scalars. you can only assign array
references. read perldoc perlreftut to get going with references. it is
a core feature you need to learn to get into perl data structures.

  HK ... and, can the elements of that array be accessed using the syntax I
  HK use  in the second print statement?

well, no as you just assigned a single string into that element. it is
not an array reference. you dereferenced it and perl says you don't have
a reference in there so die!

  HK ( $header{keys} ) = split(/\t\n/, );
  HK ( $header{info} ) = split(/\t\n/, );

this will do what you want. note the () are gone as the [] provides the
list context for split. it splits, makes a list, that is stored in the
anon array ref [] and that is stored in the hash. 

$header{info} = [ split(/\t\n/, ) ] ;

  HK print $header{info} . \n;

that would print out the string you assigned. with my change it will
print the array ref itself (not its data)

  HK print $header{info}[0] . \n;

that would dereference the string which dies. with my change it will
print the first element of the list from split.

uri

-- 
Uri Guttman  --  uri AT perlhunter DOT com  ---  http://www.perlhunter.com --
  Perl Developer Recruiting and Placement Services  -
-  Perl Code Review, Architecture, Development, Training, Support ---

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Arrow Notation vs. Colon Notation

2011-07-20 Thread Rob Dixon

On 21/07/2011 05:32, Rob Dixon wrote:


It would be possible to generate calls to undefined subroutines at
compile time, but because the symbol table can be modified at run time
with such trickery suck as *Foo::bar = \Foo::foo it is also left until
run time to report any such errors.



My apologies. This should read:

It would be possible to raise errors regarding calls to undefined
subroutines at compile time, but because the symbol table can be
modified at run time with such trickery suck as *Foo::bar = \Foo::foo
it is also left until run time to report any such errors.

Rob

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: first post

2011-07-20 Thread John W. Krahn

H Kern wrote:

Hi,


Hello,


My first newbie post. I wish to have two arrays indexed by a hash
table. The simple program below runs and behaves properly initializing
the hash table with the information I wish it to have.

However, Perl generates the following suggestion on the @header{}
assignment statements,

Scalar value @header{keys} better written as $header{keys} at
iifm.pl line...

If I rewrite it as Perl suggests, the two %header{} elements get
initialized to the size of the arrays instead of the arrays. Why does
Perl make this suggestion, and how do I get rid of it without getting
rid of the use warnings statement?

Thanks, --H



use strict;
use warnings;
my %header;

open( IN, , $ARGV[0] );

@header{keys} = split(/\t\n/, IN);
@header{info} = split(/\t\n/, IN);



The hash slice @header{keys} acts the same as a list and enforces list 
context on the assignment while the scalar $header{keys} forces scalar 
context on the assignment and as the documentation says:



perldoc -f split

split /PATTERN/,EXPR,LIMIT
split /PATTERN/,EXPR
split /PATTERN/
split   Splits the string EXPR into a list of strings and returns
that list.  By default, empty leading fields are preserved,
and empty trailing ones are deleted.  (If all fields are
empty, they are considered to be trailing.)

In scalar context, returns the number of fields found. In
scalar and void context it splits into the @_ array.  Use
of split in scalar and void context is deprecated, however,
because it clobbers your subroutine arguments.


So you need to use a scalar value but in list context:

( $header{ keys } ) = split /\t\n/, IN;
( $header{ info } ) = split /\t\n/, IN;

Or use a list slice on the right-hand side of the assignment:

$header{ keys } = ( split /\t\n/, IN )[ 0 ];
$header{ info } = ( split /\t\n/, IN )[ 0 ];





John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/