Está en la página 1de 5

Assignment-2

PART-A
Q1. If EXPR is an arbitrary expression, what is the difference between $Foo::{EXPR} and
*{"Foo::".EXPR}?
Sol:
The second would match Foo other than at the start of the record if $* were set. The deprecated
$* flag does double duty, filling the roles of both /s and /m. By using /s, you suppress any settings of
that spooky variable, and force your carets and dollars to match only at the ends of the string and not at
ends of line as well -- just as they would if $* weren't set at all.
The second is disallowed under `use strict "refs"'. Dereferencing a string with
*{"STR"} is disallowed under the refs stricture, although *{STR} would not be. This is
similar in spirit to the way ${"STR"} is always the symbol table variable, while ${STR}
may be the lexical variable.

Q2 What's the significance of @ISA, @EXPORT @EXPORT_OK %EXPORT_TAGS list & hashes in
a perl package? With example?
Sol:

Hashes are an advanced form of array. One of the limitations of an array is that the information
contained within it can be difficult to get to. For example, imagine that you have a list of people and
their ages.
The hash solves this problem very neatly by allowing us to access that @ages array not by an index,
but by a scalar key. For example to use age of different people we can use thier names as key to define
a hash.
e.g:
%ages = ('Martin' => 28,
'Sharon' => 35,
'Rikke' => 29,);

print "Rikke is $ages{Rikke} years old\n";

This will produce following result


Rikke is 29 years old

@ISA -> each package has its own @ISA array. this array keep track of classes it is
inheriting. ex: package amit;
@ISA=( parentclass); @EXPORT this array stores the subroutins to be exported from a module.
@EXPORT_OK this array stores the subroutins to be exported only on request.

PART-B
Q3. In Perl for each and for works same. Is this statement true. Justify your answer?
Sol:

For Loops
Perl's C-style for loop works like the corresponding while loop; that means that this:
for ($i = 1; $i < 10; $i++) {
...
}
is the same as this:
$i = 1;
while ($i < 10) {
...
} continue {
$i++;
}
There is one minor difference: if variables are declared with my in the initialization section of the for ,
the lexical scope of those variables is exactly the for loop (the body of the loop and the control
sections).
Besides the normal array index looping, for can lend itself to many other interesting applications.
Here's one that avoids the problem you get into if you explicitly test for end-of-file on an interactive
file descriptor causing your program to appear to hang.

Foreach Loops
The foreach loop iterates over a normal list value and sets the variable VAR to be each element of the
list in turn. If the variable is preceded with the keyword my, then it is lexically scoped, and is therefore
visible only within the loop. Otherwise, the variable is implicitly local to the loop and regains its
former value upon exiting the loop. If the variable was previously declared with my, it uses that
variable instead of the global one, but it's still localized to the loop. This implicit localisation occurs
only in a foreach loop.
The foreach keyword is actually a synonym for the for keyword, so you can use foreach for readability
or for for brevity. (Or because the Bourne shell is more familiar to you than csh, so writing for comes
more naturally.) If VAR is omitted, $_ is set to each value.
If any element of LIST is an lvalue, you can modify it by modifying VAR inside the loop. Conversely,
if any element of LIST is NOT an lvalue, any attempt to modify that element will fail. In other words,
the foreach loop index variable is an implicit alias for each item in the list that you're looping over.
If any part of LIST is an array, foreach will get very confused if you add or remove elements within the
loop body, for example with splice. So don't do that.
foreach probably won't do what you expect if VAR is a tied or other special variable. Don't do that
either.
Examples:
for (@ary) { s/foo/bar/ }

for my $elem (@elements) {


$elem *= 2;
}

for $count (10,9,8,7,6,5,4,3,2,1,'BOOM') {


print $count, "\n"; sleep(1);
}

for (1..15) { print "Merry Christmas\n"; }

foreach $item (split(/:[\\\n:]*/, $ENV{TERMCAP})) {


print "Item: $item\n";
}

it's faster because Perl executes a foreach statement more rapidly than it would the equivalent for loop.
Q4. When you are using MySQL , how do you give permissions to the users to access the tables of the
database?
Sol:

The MySQL client program, also known as the MySQL monitor, is an interface that allows the
user to connect to the MySQL server, create and modify databases, and execute queries and view their
results. This program is started by executing the command mysql at the shell prompt. In general, the
syntax for this command is:
%>mysql [options] [database]
Where [options] can be one or a series of options used in conjunction with the mysql program, and
[database] is the name of the database to use. Since it is assumed to be the reader's first time using the
MySQL monitor, take a moment to review all offered options by executing the following command:
%>mysql --help
This produces a long list of options that can be used in conjunction with the mysql program. For the
moment, however, the main goal is to simply connect to the database server. Therefore, execute the
following command:
%>mysql -u root

mysql>
Congratulations, you are now connected to the MySQL monitor as the almighty root user. Your first
official action as this supreme leader of the MySQL database server should be to ensure that nobody
else can declare this position. Therefore, make it possible to only connect as root in the future by
supplying a password. Change the password from its current blank (or null) value, to something
difficult to guess using the following command:
mysql>SET PASSWORD FOR 'root'@'localhost' = PASSWORD('secret_password');

this command will change the password by updating what are commonly known as the MySQL
privilege tables. These tables, collectively located in the mysql database, contain information regarding
the connection and usage capabilities of all users intended to use the MySQL database server. More
specifically, this command will update the user table, updating the password field of the row in which
the user field's value is 'root'. The password field will be updated with the encrypted value of the string
enclosed within the Password() function

Q5. EXPLAIN is used in query optimization, does this directly contribute in optimizing the query.?
Sol:

Yes, “EXPLAIN “ directly contribute in optimizing the query The execution plan chosen by the
optimizer for a SQL statement by using the EXPLAIN PLAN statement. When the statement is issued,
the optimizer chooses an execution plan and then inserts data describing the plan into a database table.
Simply issue the EXPLAIN PLAN statement and then query the output table.
These are the basics of using the EXPLAIN PLAN statement:
• Use the SQL script UTLXPLAN.SQL to create a sample output table called PLAN_TABLE in
your schema.
• Include the EXPLAIN PLAN FOR clause prior to the SQL statement. After issuing the
EXPLAIN PLAN statement, use one of the scripts or package provided by Oracle to display the
most recent plan table output.
• The execution order in EXPLAIN PLAN output begins with the line that is the furthest
indented to the right. The next step is the parent of that line. If two lines are indented equally,
then the top line is normally executed first.

e.g:
EXPLAIN PLAN FOR
SELECT e.employee_id, j.job_title, e.salary, d.department_name
FROM employees e, jobs j, departments d
WHERE e.employee_id < 103
AND e.job_id = j.job_id
AND e.department_id = d.department_id;

También podría gustarte