[30038] in Perl-Users-Digest
Perl-Users Digest, Issue: 1281 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Feb 15 18:09:37 2008
Date: Fri, 15 Feb 2008 15:09:05 -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 Fri, 15 Feb 2008 Volume: 11 Number: 1281
Today's topics:
Re: Can't locate object method err <abigail@abigail.be>
Re: Can't locate object method err <uri@stemsystems.com>
Re: Can't locate object method err <joost@zeekat.nl>
copying arrays <user@example.net>
Re: copying arrays <joost@zeekat.nl>
Re: copying arrays <mritty@gmail.com>
Re: dump POST data to screen <noreply@gunnar.cc>
Writers Block <dalestubblefield@gmail.com>
Re: Writers Block <joost@zeekat.nl>
Re: Writers Block <dalestubblefield@gmail.com>
Re: Writers Block (Randal L. Schwartz)
Re: Writers Block <someone@example.com>
Re: Writers Block <joost@zeekat.nl>
Re: Writers Block <dalestubblefield@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 15 Feb 2008 19:40:05 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: Can't locate object method err
Message-Id: <slrnfrbqkl.t9a.abigail@alexandra.abigail.be>
_
Joost Diepenmaat (joost@zeekat.nl) wrote on VCCLXXXI September MCMXCIII
in <URL:news:87ve4q2jo3.fsf@zeekat.nl>:
,, xhoster@gmail.com writes:
,,
,, > David Williams <dw149@acmex.gatech.edu> wrote:
,, > I'm not sure why Perl doesn't just try to do an implicit "use %s" as
,, > a last resort before failing with a 'perhaps you for forgot to lead "%s"'
,, > message. It seems to me like it would be a nice convenience, but it
,, > doesn't do that; and there is probably a reason that that would be a bad
,, > idea that I have overlooked.
,,
,, Well, it's also likely that the package name is misspelled, and blindly
,, loading the wrong module could theoretically lead to much more
,, problematic issues than simply failing.
Yeah, but one could have made said typo in the 'use' statement which could
have lead to problematic issues anyway.
Having said that, doing an implicite use statement sounds handy, and it
would have been if use statements never took arguments. But many times,
they do.
Abigail
--
perl5.004 -wMMath::BigInt -e'$^V=Math::BigInt->new(qq]$^F$^W783$[$%9889$^F47]
.qq]$|88768$^W596577669$%$^W5$^F3364$[$^W$^F$|838747$[8889739$%$|$^F673$%$^W]
.qq]98$^F76777$=56]);$^U=substr($]=>$|=>5)*(q.25..($^W=@^V))=>do{print+chr$^V
%$^U;$^V/=$^U}while$^V!=$^W'
------------------------------
Date: Fri, 15 Feb 2008 19:51:41 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Can't locate object method err
Message-Id: <x7bq6i2e2a.fsf@mail.sysarch.com>
>>>>> "A" == Abigail <abigail@abigail.be> writes:
A> in <URL:news:87ve4q2jo3.fsf@zeekat.nl>:
A> ,, xhoster@gmail.com writes:
A> ,,
A> ,, > David Williams <dw149@acmex.gatech.edu> wrote:
A> ,, > I'm not sure why Perl doesn't just try to do an implicit "use %s" as
A> ,, > a last resort before failing with a 'perhaps you for forgot to lead "%s"'
A> ,, > message. It seems to me like it would be a nice convenience, but it
A> ,, > doesn't do that; and there is probably a reason that that would be a bad
A> ,, > idea that I have overlooked.
A> ,,
A> ,, Well, it's also likely that the package name is misspelled, and blindly
A> ,, loading the wrong module could theoretically lead to much more
A> ,, problematic issues than simply failing.
A> Yeah, but one could have made said typo in the 'use' statement which could
A> have lead to problematic issues anyway.
A> Having said that, doing an implicite use statement sounds handy, and it
A> would have been if use statements never took arguments. But many times,
A> they do.
most of the time when passing args to use statements those are for
procedural imports, not OO. and missing classes in new calls are
obviously OO calls.
and there are other pitfalls to that idea. sometimes you load a module
with one package name and call new on another. a classic one is with use
IO::Socket but you call IO::Socket::INET->new.
also since there is no direct relationship between package and file
names (other than the implied import call in use), there is no way to
say if a missing class name is to be found in the module of the same
name. and since OO calls can be based on dynamic class names and classes
can be made on the fly, there are many ways to fail that will not be
fixed with an implied require call (shouldn't be a use since it is at
run time).
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Architecture, Development, Training, Support, Code Review ------
----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Fri, 15 Feb 2008 20:54:35 +0100
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: Can't locate object method err
Message-Id: <87ir0q2dxg.fsf@zeekat.nl>
Abigail <abigail@abigail.be> writes:
> Yeah, but one could have made said typo in the 'use' statement which could
> have lead to problematic issues anyway.
Sure, but since the same package name is generally used at least as many
times in method calls as in "use" statements, the chances of mis-typing
it in one (of possibly many) method calls is higher.
> Having said that, doing an implicite use statement sounds handy, and it
> would have been if use statements never took arguments. But many times,
> they do.
That's true. I hadn't even thought of that. Not to mention the modules
that initialize a whole set of classes from a single "use" statement.
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
------------------------------
Date: Fri, 15 Feb 2008 14:11:33 -0500
From: monkeys paw <user@example.net>
Subject: copying arrays
Message-Id: <6Mednb-9qNZ5fijanZ2dnUVZ_hynnZ2d@insightbb.com>
In the following code the array @drop is modified as well
as @drop2. How can I achieve modifying @drop2 without affecting
@drop?
> @drop = (
> { 'hello' => 'world', 'hi' => 'there' },
> { 'hello' => 'dude', 'hi' => 'ma' },
> );
>
> @drop2 = @drop;
> for (@drop2) {
> delete $_->{hi};
> }
>
> use Data::Dumper;
> die Dumper(\@drop) . Dumper(\@drop2);
------------------------------
Date: Fri, 15 Feb 2008 20:15:34 +0100
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: copying arrays
Message-Id: <87myq22fqh.fsf@zeekat.nl>
monkeys paw <user@example.net> writes:
> In the following code the array @drop is modified as well
> as @drop2. How can I achieve modifying @drop2 without affecting
> @drop?
Actually, neither array is modified. You'd modifying the hashes
referenced in both arrays. In other words, you want a deep copy of the
structure, not just a copy of the references in the array. In this
specific case:
my @drop2 = map { { %$_ } } @drop;
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
------------------------------
Date: Fri, 15 Feb 2008 11:27:17 -0800 (PST)
From: Paul Lalli <mritty@gmail.com>
Subject: Re: copying arrays
Message-Id: <e8af65fa-c3d7-4cf5-907a-4f458475173c@e25g2000prg.googlegroups.com>
On Feb 15, 2:15=A0pm, Joost Diepenmaat <jo...@zeekat.nl> wrote:
> monkeys paw <u...@example.net> writes:
> > In the following code the array @drop is modified as well
> > as @drop2. How can I achieve modifying @drop2 without affecting
> > @drop?
>
> Actually, neither array is modified. You'd modifying the hashes
> referenced in both arrays. In other words, you want a deep copy of the
> structure, not just a copy of the references in the array. In this
> specific case:
>
> my @drop2 =3D map { { %$_ } } @drop;
And in the more general case, when the first "inner" level of the
structure may also be references, use the Storable module, as
suggested by `perldoc -q copy`:
use Storable qw/dclone/;
my @drop2 =3D @{ dclone(\@drop) };
Paul Lalli
------------------------------
Date: Fri, 15 Feb 2008 21:08:23 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: dump POST data to screen
Message-Id: <61m9lvF1vsqklU1@mid.individual.net>
henribaeyens wrote:
> On Fri, 15 Feb 2008 18:16:38 +0100, Gunnar Hjalmarsson wrote:
>> henribaeyens wrote:
>>> I'm trying to figure out a way to output, for debugging purposes, the
>>> contents of a multipart form-data.
>>
>> print <STDIN>;
>
> nice guru shit!
>
> I managed to come up with
>
> while(<STDIN>) {
> chomp($_);
> print "$_<br>\n";
> }
You didn't mention that you are using a CGI script to inspect the data.
It's not advisable to display unparsed form data as HTML. If you are
using a browser, try this instead:
print "Content-type: text/plain\n\n", <STDIN>;
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Fri, 15 Feb 2008 12:41:19 -0800 (PST)
From: dale <dalestubblefield@gmail.com>
Subject: Writers Block
Message-Id: <a3d368e9-9ba2-471d-a6c9-476dc28d8283@o10g2000hsf.googlegroups.com>
Here's what I'm trying to do in this block of code - I'm trying to get
the UIDs from /etc/passwd and present them to the user. Then, the
user can pick a UID not in the list. If the user picks a UID in the
list, user should have his hand slapped and be prompted to enter a new
value.
The "Get UID from user" and "Check to make sure it's a new UID" parts
are what I am having trouble with.
Here is the code:
############################################## GET UID #########
#
### Get /etc/passwd names and UIDs
my $toBe = "/etc/passwd";
my $copy = "/root/bin/temp";
copy($toBe, $copy) or die "\n Cannot open file \n";
`awk -F : '{print \$1"," \$3}' /root/bin/temp > tempUID`;
`rm -f /root/bin/temp`;
### Read login names and UIDs into hash
my $tempUID = "/root/bin/tempUID";
open(FILE,"<$tempUID") || die "\nCan't read: $tempUID$!\n";
my $uid;
my $login;
while(<FILE>){
($login,$uid)=split(/,/,$_);
chomp $uid;
$hashUID->{$uid} = $login;}
### Print UIDs
$counter = 0; # reset counter
while (($uid, $login) = each(%$hashUID))
{
if ( ($uid >500)&&($uid < 5000) ){
print "\n\t$uid \t$login";}
}
### Get UID from user
sub get_UID{
print "\n\n These are the non-system logins on this system.
\n Please input a new UID to start your users with: ";
$uidInput=<>;
chomp $uidInput;}
get_UID();
### Check to make sure it's a new UID
foreach (keys (%$hashUID)) {
print "\nchecking UID"; ##for debugging
if ($uidInput == $uid) {
print "\n That UID exists already. Try another: ";
goto &get_UID;}
}
#
################################################################
------------------------------
Date: Fri, 15 Feb 2008 21:45:40 +0100
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: Writers Block
Message-Id: <871w7e2bkb.fsf@zeekat.nl>
dale <dalestubblefield@gmail.com> writes:
> Here's what I'm trying to do in this block of code - I'm trying to get
> the UIDs from /etc/passwd and present them to the user. Then, the
> user can pick a UID not in the list. If the user picks a UID in the
> list, user should have his hand slapped and be prompted to enter a new
> value.
>
> The "Get UID from user" and "Check to make sure it's a new UID" parts
> are what I am having trouble with.
Without any other info on what the trouble is:
http://perl.plover.com/FAQs/Buffering.html
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
------------------------------
Date: Fri, 15 Feb 2008 12:58:00 -0800 (PST)
From: dale <dalestubblefield@gmail.com>
Subject: Re: Writers Block
Message-Id: <dc41beeb-c881-448b-9e6b-ecc53e9f3dc9@62g2000hsn.googlegroups.com>
On Feb 15, 2:45 pm, Joost Diepenmaat <jo...@zeekat.nl> wrote:
> dale <dalestubblefi...@gmail.com> writes:
> > Here's what I'm trying to do in this block of code - I'm trying to get
> > the UIDs from /etc/passwd and present them to the user. Then, the
> > user can pick a UID not in the list. If the user picks a UID in the
> > list, user should have his hand slapped and be prompted to enter a new
> > value.
>
> > The "Get UID from user" and "Check to make sure it's a new UID" parts
> > are what I am having trouble with.
>
> Without any other info on what the trouble is:
>
> http://perl.plover.com/FAQs/Buffering.html
I should note that this is my first attempt at a PERL script. I have
hit writer's block due to my lack of PERL knowledge and I am not sure
if I am checking the user's input correctly.
I skimmed over the page you referenced, but I do not understand how it
applies in this situation.
thanks,
-dale-
------------------------------
Date: Fri, 15 Feb 2008 13:31:03 -0800
From: merlyn@stonehenge.com (Randal L. Schwartz)
To: dale <dalestubblefield@gmail.com>
Subject: Re: Writers Block
Message-Id: <864pc9x5yg.fsf@blue.stonehenge.com>
>>>>> "dale" == dale <dalestubblefield@gmail.com> writes:
dale> I should note that this is my first attempt at a PERL script.
Unless you're talking about shooting a commercial for the
non-profit "Protecting Escarpment Rural Lands" (perlofburlington.org),
you are probably working on a Perl script, and not a "PERL script".
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
------------------------------
Date: Fri, 15 Feb 2008 21:45:00 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Writers Block
Message-Id: <wJntj.22015$w57.9288@edtnps90>
dale wrote:
> Here's what I'm trying to do in this block of code - I'm trying to get
> the UIDs from /etc/passwd and present them to the user. Then, the
> user can pick a UID not in the list. If the user picks a UID in the
> list, user should have his hand slapped and be prompted to enter a new
> value.
>
> The "Get UID from user" and "Check to make sure it's a new UID" parts
> are what I am having trouble with.
>
>
> Here is the code:
[ SNIP ]
Try something like this:
#!/usr/bin/perl
use warnings;
use strict;
my %hashUID;
while ( my ( $name, $uid ) = ( getpwent )[ 0, 2 ] ) {
$hashUID{ $uid } = $name;
}
### Print UIDs
while ( my ( $uid, $login ) = each %hashUID ) {
if ( $uid > 500 && $uid < 5000 ) {
print "\t$uid \t$login\n";
}
}
print "\nThese are the non-system logins on this system.\nPlease input a
new UID to start your users with: ";
{
( my $uidInput = <STDIN> ) =~ tr/0-9//cd;
if ( $uidInput <= 500 || $uidInput >= 5000 || exists $hashUID{
$uidInput } ) {
print "\nThat UID exists already. Try another: ";
redo;
}
}
__END__
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
------------------------------
Date: Fri, 15 Feb 2008 22:47:48 +0100
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: Writers Block
Message-Id: <87ir0p28or.fsf@zeekat.nl>
dale <dalestubblefield@gmail.com> writes:
> I skimmed over the page you referenced, but I do not understand how it
> applies in this situation.
I'm assuming (since you didn't make it clear) that the "print prompt,
read input" parts is where the problem is, and that the problem is that
you don't actually see the prompt.
Please correct me if that's not the case.
From that assumption, the problem is that perl is buffering your
script's output and you should switch off output buffering. Put
$|=1;
At the top of the code (or at least, before the prompting code).
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
------------------------------
Date: Fri, 15 Feb 2008 14:50:36 -0800 (PST)
From: dale <dalestubblefield@gmail.com>
Subject: Re: Writers Block
Message-Id: <fbe20fd5-9931-4298-9e20-07ddb732080e@v3g2000hsc.googlegroups.com>
On Feb 15, 3:47 pm, Joost Diepenmaat <jo...@zeekat.nl> wrote:
> dale <dalestubblefi...@gmail.com> writes:
> > I skimmed over the page you referenced, but I do not understand how it
> > applies in this situation.
>
> I'm assuming (since you didn't make it clear) that the "print prompt,
> read input" parts is where the problem is, and that the problem is that
> you don't actually see the prompt.
>
> Please correct me if that's not the case.
>
[SNIP]
The code I was using and the code posted by John Krahn both work
(Thanks John). In other words, they both will accept numerical input
from the user and store the inputted value in $uidInput.
The problem is that I want the user to be prompted to enter a
different UID if the UID already exists in %hashUID (which is just a
copy of username and uid values from /etc/passwd). I am having
problem with this error checking portion.
Overall, what I would like this script to do is allow a user to create
users on a Linux server in batch from a CSV file (to save me time).
Obviously, a UID that is already in use cannot be used by a newly
created user. On the other hand, the user is encouraged to reuse a
GID (which is what I will probably do 9/10 of the time). In the
future, I would like to also allow the user to create a new group with
this script, but that is outside of the scope of what I am currently
trying to accomplish.
Perhaps knowing my overall goal and seeing my code in it's entirety
would allow anyone to assist me further. I realize there may be some
better way to do things in this script, such as how I awk files with
Perl instead of issuing Bash commands. Hopefully, I will be able to
streamline those things as my Perl skills get better. I just started
learning Perl on Monday. Any suggestions for optimization are
certainly welcome.
Anyway, here is the code:
#!/usr/bin/perl
# Author: Dale Stubblefield
# Purpose: To add multiple users from an input file.
# input file syntax: "login,user name"
#
use warnings;
use strict;
use File::Copy;
# Declare Variables
my $hash;
my $hashGID;
my %hashUID;
my $uid;
my $uidInput;
my $counter = 0;
# Clear the screen
system("clear");
############################# Get input file from user #########
#
print "\n\n Please provide a file with logins and names.";
print "\n\n (File syntax: ag03555,Jack ) ";
print "\n\n (FILE Input example: /root/fake.list ) ";
print "\n\n FILE: ";
my $FILE=<>;
open(FILE,"<$FILE") || die "\nCan't read: $FILE$!\n";
while(<FILE>){
my($login,$name)=split(/,/,$_);
chomp $name;
$hash->{$login} = $name;
}
close(FILE);
#
###########################################################
############################################## GET GID #########
#
### Get /etc/group names and GIDs
my $toBe = "/etc/group";
my $copy = "/root/bin/temp";
copy($toBe, $copy) or die "\n Cannot open file \n";
`awk -F : '{print \$1"," \$3}' /root/bin/temp > /root/bin/tempGID`;
`rm -f /root/bin/temp`;
### FOR DEBUGGING ###
# print $tempGID;
#####################
### Read group names and GIDs into hash
my $tempGID = "/root/bin/tempGID";
open(FILE,"<$tempGID") || die "\nCan't read: $tempGID$!\n";
my $gid;
my $group;
while(<FILE>){
($group,$gid)=split(/,/,$_);
chomp $gid;
$hashGID->{$gid} = $group;
}
close(FILE);
### Print GIDs
print "\n These are the non-system groups on this system.";
print "\n\tGID\tGroupname";
while (($gid, $group) = each(%$hashGID)){
if ( ($gid >500)&&($gid < 5000) ){
print "\n\t$gid \t$group";}
}
### Get GID from user
print "\n\n Please select a GID to add your users to: ";
my $gidInput=<>;
chomp $gidInput;
#
###########################################################
############################################## GET UID #########
#
### Get /etc/passwd names and UIDs
my $toBe = "/etc/passwd";
my $copy = "/root/bin/temp";
copy($toBe, $copy) or die "\n Cannot open file \n";
`awk -F : '{print \$1"," \$3}' /root/bin/temp > /root/bin/tempUID`;
`rm -f /root/bin/temp`;
### Read login names and UIDs into hash
my $tempUID = "/root/bin/tempUID";
open(FILE,"<$tempUID") || die "\nCan't read: $tempUID$!\n";
my $uid;
my $login;
while(<FILE>){
($login,$uid)=split(/,/,$_);
chomp $uid;
%hashUID->{$uid} = $login;}
### Print UIDs
print "\n\n These are the non-system logins on this system.\n";
while (($uid, $login) = each(%hashUID)){
if ( ($uid >500)&&($uid < 5000) ){
print "\n\t$uid \t$login";}
}
### Get UID from user
print "\n\n Please input a new UID to start your users with: ";
my $uidInput;
#chomp $uidInput;
{
( $uidInput = <STDIN> ) =~ tr/0-9//cd;
if ( ($uidInput <= 500 || $uidInput >= 5000 ) ||exists
$hashUID{uidInput} ) {
print "\n That UID already exists or is out of range. Try another:
";
redo;
}
}
#
###########################################################
######################################### Create Users #########
#
$counter = 0; # reset counter
foreach (keys (%$hash)){
# -u = UID
# -g = GID
# -c = comments/user real name
print "\n useradd $_ -u $uidInput -g $gidInput -c \"$hash->{$_}
\" ";
print "\n echo changeme | passwd --stdin $_ ";
print "\n chage -d 2007-10-10 -W 14 -M 90 $_ \n";
print "\n";
##### Left commented out for debugging
##### so users aren't actually created
# `useradd $_ -u $uid -g $gidInput -c \"$hash->{$_}\"`;
# `echo changeme | passwd --stdin $_`;
# `chage -d 2007-10-10 -W 14 -M 90 $_`;
######################################
$uid ++;
$counter ++;
}
print " $counter users were added.\n";
print "\n";
#
###########################################################
############################################# Clean up #########
`rm -f /root/bin/temp`;
`rm -f /root/bin/tempGID`;
`rm -f /root/bin/tempUID`;
###########################################################
------------------------------
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 1281
***************************************