Wednesday, January 04, 2006
IFS and read
IFS=','
while IFS=':' read -A line
do
end=$(( ${#line[*]} - 1 ))
if [[ ${line[$end]} != /sbin/nologin ]]
then
echo "${line[*]}"
fi
done < /etc/passwd
(display all the lines in /etc/passwd whose last field is not /sbin/nologin, changing the field delimiter from colon to comma).
The first IFS=',' sets IFS for the echo expansion and is not overridden by the IFS on the read line. That IFS only applies for the read, nothing else.
By the way, did you know that the expansion (including $*) uses the first character from IFS, so order matters? So IFS=',.:' gives different results to IFS=':,.'.
All this applies to the standard POSIX shell, as well as Korn shell and Bash, and I was alerted to these features by Classic Shell Scripting (see a previous post).
Finally, a significant bug in pdksh has been fixed in the version with Fedora core 4 (1993-12-28 q). In early versions a pipe generated a child process on each side, so piping into a while loop was not useful, since the while loop ran in a different process to the rest of the script, and so variables could not be saved. Now it all works, consider:
#!/bin/ksh
ps -ef | while read uid pid ppid c stime tty time cmd
do
if [[ $cmd == *xinetd* ]]
then
break
fi
done
echo "Pid of xinetd is: $pid"
Unfortunately this will still not work in Bash, for the same reason as it did not work in the old pdksh. In Bash we can use process substitution instead, and while we are about it we may as well use an ERE (because we can):
while read uid pid ppid c stime tty time cmd
do
if [[ $cmd =~ '^xinetd +' ]]
then
break
fi
done < <(ps -ef)
echo "Pid of xinetd is: $pid"
The man pages for the new pdksh say process substitution works in Korn shell as well, and I am told this works on Solaris, but it does not appear to work on Linux.
Friday, December 30, 2005
Toys for boys
Good views of Mars in November and early December, now good views of Saturn, and various nebula like M42 and M37. OK, so what's that got to do with programming and stuff?
Well, most modern telescopes have an electronic control called a "goto" system - you get the idea. This is normally controlled by a hand-held device, but can be controlled by a PC using an Open Source object model named ASCOM - Astronomy Common Object Module.
http://ascom-standards.org/faq.html
Currently it appears to be written using COM, which of course is proprietry and realistically only runs on one of the many Microsoft operating systems. OK, you can get it to run on others, but would you want to, really?
This is one I will be looking at further.
Wednesday, December 21, 2005
Switching off terminal echo in Perl
use Term::ReadKey;
print 'Password: ';
ReadMode 'noecho';
my $password = ReadLine; # Corrected
ReadMode 'normal';
chomp $password;
print "Password was: $password\n";
Another delegate said something like "I guess another module will replace characters typed with asterisks". Well, that is not so simple. After some experimentation I came up with the following script, which has to use raw terminal input and take iinto account differences between Windows and Unix/Linux:
use Term::ReadKey;
use strict;
local $| = 1;
print 'Password: ';
my $password = '';
my $retn = $^O eq 'MSWin32'?"\r":"\n";
ReadMode 'raw';
while (1)
{
my $key;
# Loop waiting for a key to be pressed
while (!defined $key)
{
$key = ReadKey -1;
}
last if $key eq $retn;
$password .= $key;
print '*';
}
ReadMode 'normal';
print "\n";
print "Password was: $password\n";
Check the Term::ReadKey documentation for the arguments. Note that I have to wait for a key to be pressed this time, and write an unbuffered '*' (local $| = 1 makes STDOUT unbuffered).
Wednesday, November 16, 2005
Perl subroutine parameter passing - again
One of the reasons Damian Conway does not like prototypes is that (his words) they do not give the results expected. Well, what results do you expect when you don't use them?
It is sometimes said that parameters are passed into Perl subroutines by copy, and some say they are passed by reference. In fact they are passed by magic, and as we all know, any technology sufficiently advanced is indistinguishable from Perl.
What do you expect to be printed?
sub mysub
{
@_ = qw(one two three)
}
@array = qw(The quick brown fox);
mysub (@array);
print "@array\n";
We get 'The quick brown fox '. The array is unchanged, so you might think the array is passed by value. The call:
mysub (qw(The quick brown fox));
also works fine, so no problem. Let's change the subroutine to use a slice instead:
sub mysub
{
@_[0,1,2] = qw(one two three)
}
Now we get 'one two three fox', the array is changed! Changing the elements changes the caller's array, changing the whole array does nothing! Now the line:
mysub (qw(The quick brown fox));
fails with "Modification of a read-only value attempted…"
This is a run-time error, whereas if you use a prototype:
sub mysub (\@)
we get a compile-time error:
"… must be an array (not list)…"
If I preferred run-time over compile-time errors I would be using the Korn shell.
Once the prototype has been specified it does not matter if we alter the caller's array in total or use a slice – the caller's code is always the same.
OK, I admit that altering @_ direct is damn awful code, and it breaks another of Damian's rules. He's not all bad ;-)
I don't believe that an inconsistency in one part of Perl (prototypes) is a justification to switch to an inconsistency in another (pass by magic). Just admit the inconsistencies and celebrate them – or move to Perl 6.
Thursday, October 20, 2005
Perl 6 at EuroOSCON
This is a brief summary of my notes from the Perl 6 and pugs sessions. I have only included the stuff that was new compared to the Perl 6 Appendix in the 'Perl Programming' course. I might have repeated myself.
use strict and use warnings are on by default;
Quotes are only required around a hash key when {} are used.
$hash{'key'} becomes %hash
Barewords are not allowed. Ever. Even for file handles.
fail{…} warn{…} die{…} throw a 'not yet implemented' type warning – the Perl 6 developers must be using that a lot ;-)
Many improvements to interpolation:
"{ executable code like a do block} "
Subroutine calls are allowed: "&mysub(args)"
Interpolate an array: "@array[]"
Interpolate a hash: "%hash{}"
sprintf is probably obsolete, printf definitely is:
print $var.as('%6.2f');
print $hash.as("%-20s: %2.6f", "\n");
Control can be made over exactly what is interpolated:
qq:c(0)/ / # don't interpolate
qq:s(1)/ / # only interpolate scalars
qq:s(1):a(1)/ / # interpolate scalars and arrays
hummmmmmm
Here document syntax is changed:
my $var = q:to /END/ # s(1):a(1):h(1) can be added
…
END
Not sure where the semi-colon goes
Ranges have a new feature:
x..^y means from x up to y-1
x^..^y means from x+1 to y-1
To open a file with an automatic chomp::
my $fh = open $filename
This can be overridden.
while (<$fh>) {…}
becomes:
while = $fh {…}
while (<>){…}
becomes
while = $ARGS{…} or while =<> {…}
=<> is known as the 'fish' operator
There is now a 'prompt' verb
Most built-ins that operator on scalars and arrays are now methods:
$var.substr(…) # returns the substring
$var .= $var.substr(…) # changes the substring
No need for Data::Dumper, instead:
$var.perl()
And there is much more, Damian could not complete his talk because of time, and it is not downloadable. I'll keep you posted as I play with pugs.
Wednesday, October 19, 2005
EuroOSCON - whatever. Is Perl 6 too complex?
Q. Will Perl 6 still be suitable for guys who just want a better language that ksh or .bat files?
A. The easy things will still be easy
Q. Will Perl 6 still be suitable for sys.admin's?
A. Hey! I'm still a sys.admin.
Me: No you are not, you are a language demi-god.
A. Shrug. Sys.Admins will still be able to use it.
Q. Are you saying that because that is what I want to hear?
A. Sys.Admins will still be able to use it.
Thank-you very much.
We also discussed training strategies for Perl 6, and even for Perl 5, and I wasn't that hard on him. Larry Wall is a very nice chap (oh, I said that).
EuroOSCON 6 - Perl 6 Update
EuroOSCON 5 - Installation Installation Installation
BUT...
Trying to install it is a nighmare. There is no README file, so no list of dependancies. I start downloading and installing them by hand and choke when I come to Catalyst. So I switch off my firewall (oooooooh) and run cpan, and nothing installs. Tried ppm - the same. Everything downloads OK, but every single one fails on the make - it should be using nmake on windows.
Doing this manually would take for ever, so I give up.
Other Open Source products seem to suffer from the same thing, and it is about time the community did something about it. OK, that means you and me. Something is being done about it, there are many people working on this. I appreciate the complexity, but I don't think the underlying issue is technical. It is trying to bring order to the bazaar, and I am not sure that is possible without turning it into M&S.
Tuesday, October 18, 2005
EuroOSCON 4 - 'Ere we go, 'Ere we go, 'Ere we go!
O'Reilly
Book sales year on year current figures:
C# down 3%
Python up 30%
VB down 1%
Perl up 4%
Ruby up > %1000
Linux(generic) down 4%
Read Hat down 24%
Other Linux/Unix up 2,807%
Now does that tell several stories?
Waugh
Microsoft dialog boxes: Q. "Would you like tea or coffee?
The other talks were just saying what a wonderful thing Open Source is, and how it is the future. Yeh Yeh Yeh.
Perl 6
Lots of detail, I have to sort out my notes in detail - another post tomorrow.
Damian and Larry's presentation was titled "Perl 6: End-game"
Damian Conway is a trainer, and is giving 2-day Perl 6 courses NOW, and expects to be giving full blown Perl 6 courses next summer.
Have more sessions this evening, tomorrow is quieter.
EuroOSCON 3 - Perl Best Practices (2) - prototypes
Damian Conway is dead against prototypes, and says they should all die a horrible death in a pit filled with spikes.
And snakes (I forgot about the snakes).
Prototypes on $@% are rubbish – I agree, but \$\@\% have some advantages.
Take the swap_arrays code in his book (p.196), let's think of some simple user mistookes:
Without prototypes:
swap_arrays(@sheep, @goats); # Missed out the \
Without prototypes, provided you are using strict, you get:
Can't use string ("Blackface") as an ARRAY ref while "strict refs" in use at …
BUT the error is given as being in the subroutine, not at the call (where the actual error is). perl also stops right there, it does not detect that the next argument is wrong as well – we need to do another run to find that.
Mostly if we supply the wrong type, use strict 'refs' will save our bacon (or mutton). BUT:
swap_arrays([qw(one two)], [qw(three four)]);
does not give us an error, surprisingly. OK, at the end of the day we should not be calling a subroutine we do not understand, but if a user can, she will. If she thinks that the two lists are flattened (Damian's assumption with prototypes), then she clearly does not understand the thing anyhow.
With prototypes:
swap_arrays(\@sheep, \@goats); # Added a \
With a prototype we get an error for each argument at fault:
Type of arg 1 to main::swap_arrays must be array (not reference constructor)
Type of arg 2 to main::swap_arrays must be array (not reference constructor)
Execution of C:\proto.pl aborted due to compilation errors.
This time the error is reported correctly.
Also, with:
swap_arrays([qw(one two)], [qw(three four)]);
and a prototype we get a similar error message, again identified with the correct line number.
And I nearly forgot, empty prototypes are the only way I know (other than hand-coding) of preventing any arguments being passed, as in:
sub void_sub () { ,,, }
Calling this subroutine would give an error at the point of the erroneous call.
The Volcano beats Joe
Let's face it though, Damian is right. If you want a standard there cannot be exceptions. The problem with prototypes is that everyone thinks they are like C/C++ prototypes, but most people don't even understand THEM. Take:
size_t myfunc (char string[4]);
or
int another ();
Neither do what most people think they do, and are misleading. So how do you expect to understand Perl prototypes on the same (false) basis? KISS.Monday, October 17, 2005
EuroOSCON 2 - Perl Best Practices
A lot of great ideas and tips, methinks I need an extra slide at the end of each chapter on style, or something like it. Do you think people will mine a six day course? That's the problem, I keep picking up more stuff to put into the course!
At least I found something to take out - subroutine prototypes.
A packed room, with lots of experts, and it appears I was the only person who understood prototypes (except Damian, of course). That is scary, and probably indicates we sould not spend so much time on them in our course. Damian hates them, I just find them 'flaky' personally - but no more flaky than lots of other parts of Perl. Then again, it is Damian's mission to destroy flaky Perl, and we have to start somewhere...
I got a chance for a brief chat with Mr. Conway (he comes from that country which just lost the ashes), but I have more questions for him. Here is a reminder for me:
Why use -t *STDIN instead of STDIN?
Shouldn't IO::Prompt offer the locale specific Y/N?
An idea for a pragma - RegExp::NoCapture - make all parenth. groups non-capturing. Could REs be optimised better?
Some good speakers tonight - Damian Conway (again) and Larry Wall - really looking forward to that.
BTW - am I grateful for downloading FireFox? The W/Lan here does not work with MS Internet Explorer. Apparently it is the same problem as I mentioned with ActiveState (see below), Or so they say.....
EuroOSCON 1 - DBI
So what did find out?
First, my Perl knowledge is not so bad ;-) It is always worrying going up against a bunch of enthusiastic geeks, but hey, I don't think I let the side down. That was a confidence boost (even lecturers need that sometimes)
Next: dbi:DBM: is a prototyping database bundled with DBI, looks great for quick training demos
Next: queries can be done against a dbh, but this is not efficient (we don't mention that)
Next: if $sth->disconnect is not done, you can get a memory leak
Next: placeholders are very cool if you know how to use them (I feel lots of example slides coming on)
Next: I had missed dbh attributes before (set in the connect)
Next: Lots of things about RaiseError
Next: $sth metadata hashes are tied, so using these invoke method calls
Next: and other stuff as well
A lot I already knew, like American jokes don't work on a European audience.
Friday, October 14, 2005
Fedora 4 /etc/profile
The /etc/profile file is the first file executed by a Korn shell session on login, but it has been hijacked by Bash. On Linux the Korn shell is not used much, except by training companies of course. I doubt that /etc/profile is ever tested with ksh, so it is not surprising there is a bug, however it is a shame that the mistake is such a simple one - doesn't give one much confidence.
On sign-on to ksh we get a syntax error in /etc/profile, on the line:
if [ $EUID = 0 ]; then
The EUID shell variable is set by Bash, and is a read-only value giving the effective user-id (if you want to know the difference between effective and real user-ids, come on QAs Unix Programming course). The code is checking for a 'root' user. On Korn shell this variable is not set.
The fix is quite easy, quote the variable:
if [ "$EUID" = 0 ]; then
ALWAYS quote variables in single [. One of the effects of using [[ is that quotes are not necessary around a variable, and if the authors had come on QAs Mastering BASH Shell Scripts course, they would have known that.
Tuesday, October 04, 2005
Sneaky bat files
The structure of these bat files is as follows:
@rem = '--*-Perl-*--
perl -x -S "%0" %*
goto endofperl
@rem ';
#!/usr/local/bin/perl -w
#line 9
... Perl code ...
__END__
:endofperl
The "%0" is the name of the current bat file, and %* is a list of the arguments passed into the bat file (like $0 and $* in the Korn shell and Bash).
So we are executing the bat file as if it was a perl script! This is where the –x option to perl comes in. Check perlrun, -x means that the perl code starts at the #! line, and preceding lines are to be ignored. The –S option means that %path% is used to find the perl script. There is a problem with this.
The quotes around "%0" would appear to protect us from imbedded spaces in the filename, but they do not. Paths with imbedded spaces do not work with this method. Strangely, this works if the quotes are omitted. The implication is that most of the ActivePerl bat files do not work with imbedded spaces in path names.
(The pugs Windows installation has a similar problem)
The –S option is not really needed. This is because to run the bat file we need to specify a hierarchic or full pathname anyway (Explorer makes the script's directory its current directory).
The sneaky part is the use of @rem.
The bat file starts with a non-echo'ed remark:
@rem = '--*-Perl-*--
… bat file stuff …
@rem ';
The idea here is that, by a happy coincidence, @rem comment prefix in a bat file is a valid perl command, to assign to an array called @rem. The assignment ends on the final comment, with a close quote (notice that the first @rem has no close quote).
I can only guess that this is in the bat files for historical reasons, since the –x option to perl makes this trickery unnecessary. The goto (whatever that is) is required to leap over the perl code.
The #line directive sets the line number when error reporting, or for the die statement. This means that we can set the line numbers relative to the start of our bat file, rather than the start of the perl code.
Finally, the __END__ label tells perl that this is the end of the script, and ignores any junk that follows.
A link of interest
Will Linux Benefit from Microsoft's SNAFU in Massachusetts? by Tom Adelstein -- Microsoft had everything to lose in Massachusetts and did.
Friday, September 23, 2005
Unix Shell Scripts
I have been teaching our Korn shell course this week and I enjoyed it, but....
Why do so many people think that Korn shell and Bash are the same?
Bash is hugely more powerful than ksh, with Extended Regular Expressions in version 3, and possibly associative arrays soon (looking at the source, it seems most of the code is already there). There are still things missing, for example ERE matching is supported, so wouldn't a substition statement be a small further step?
Yes I know, danger of feature bloat. With a little more work Bash could be the new Perl 5 ;-) There might actually be a place for that once (if) Perl 6 takes hold (double smiley).
Funny how conventional programmers tend to look down on shell scripting. And funny how system administrators, and their ilk, are expected to write scripts with no training on good programming techniques and practice. Companies spend huge amounts making sure their applications are fast and efficient, then some poor erk runs a badly written script and screws the entire system. Is it the erk's fault? Maybe, but more likely the person who put them in that position.
Here is my contention: anyone can write a shell script, but it takes skill and knowledge to write a good one.
I recommend: Classic Shell Scripting by Robbins and Beebe (O'Reilly) May 2005. The best book on the subject I have come across.
Thursday, September 08, 2005
Redemption
http://www.microsoft.com/technet/scriptcenter/scripts/perl/prlindex.mspx
They only use Windows Management Instrumentation (WMI), and they are a bit thin of explaination of the code, but it's a start. One day, maybe, I will get to write my "Perl Programming for Microsoft Windows" course.
Wednesday, September 07, 2005
Bye bye IE, hello Firefox
I'm all for security, but preventing access to web pages on the local machine, even when it is not connected to the web? Give me a break!
So, hello Firefox! I used it months ago, but never got around to installing it on my nice new machine. Wel now I had a real need, and it works a treat! I am just ashamed I took so long.
So Mister Microsoft, if you want to know why I am not using your product, it is simply because it does not work - nothing personal ;-)
Wednesday, August 31, 2005
Win32::StreamNames
OK, I'll come clean. Yes it's one of mine. I was proudly showing off my blanket trick (see below) to another QA instructor when he said "I suppose you use a stream". Well, aside from a small river, a stream to me is an IO channel, or maybe a Unix device driver STREAM. This is neither. Enhanced at NTFS5 at Windows NT 5, correction, Windows 2000 ;-), a Windows File object points to Stream control blocks, which can include a Named stream. The names are of the form file-name:stream-name:$DATA. They are not normally visible to Windows Explorer, the dir command, Perl globbing, or readdir (using Win32 API). BackupRead Win32 API can retrieve the stream names, although it is not easy to use. Hence the module.
See it at your local CPAN mirror today! http://cpan.search.org
Friday, August 19, 2005
Blanket
The method I used (I couldn't remember Damian's exact code) is to take the ordinal number of each character in the character set, and write that number of space characters to the file.
For example, 'A' is decimal 65 in the character set (ISO Latin 1 and ASCII), so for that character I write a line contining 65 spaces. To get the number I use the Perl built-in function 'ord'. To decode I just have to get the length of each line and feed that into the 'chr' function to get back the original.
I showed this to my class as an ammusement, and one delegate (thank-you Daniel) played around with it. He discovered that if you zip the blanked file you get a very small archive. Smaller (about two-thirds) the size of the original, zipped. Must be the way the zip algorithm works with repeated characters.
I have just thought of another tweak I can do .............